Feat. RQ: delegation alias

Carlos noone at nowhere.com
Mon May 15 17:12:03 PDT 2006


I see thanks for the clarification.

Whats attribute lookup about ?

Daniel Keep wrote:
> 
> Carlos wrote:
>> Why wouldn't you make B inherit from A ?
>>
> 
> Adapters.  For example, let's say you've got these two classes:
> 
> class Switch
> {
>     void toggle() { ... }
> }
> 
> class PushButton
> {
>     void toggle() { ... }
> }
> 
> Now, let's say you have a PushButton, but you need to pass something a
> Switch.  They're semantically the same, they've even got the same
> members, but you just can't do it.  You can't subclass them BOTH since D
> doesn't allow multiple inheritance, and whoever wrote Switch didn't
> think to use an interface that could be implemented either.
> 
> The solution here is to write an object that adapts the PushButton into
> a Switch.
> 
> class PushButtonSwitch : Switch
> {
>     PushButton realButton;
>     void toggle() { realButton.toggle(); }
> }
> 
> That's what he's referring to.  At least, I think it is.
> 
>> Or make those methods a mixin ?
> 
> The problem with that is that you've still got to write the methods in
> the first place.  If you need to adapt several different objects, but
> only once a piece, then the mixin is a waste of time.  His suggestion is
> to automate writing the trivial (but repetitive an annoying) code to
> redirect the calls.
> 
> Now, MY solution would be to allow objects to override attribute lookup,
> but then this ain't Python :P
> 
> 	-- Daniel
> 
>> Frank Benoit wrote:
>>> In the Java Eclipse IDE, there is a refactoring called Delegate:
>>> generate Methods to forward the call to a member variable. This is good
>>> because you don't have to write so much, this is bad because there is so
>>> much code, doing nothing :)
>>>
>>> How about making this into the D language:
>>>
>>> class A{
>>>     int getPrice(){ return 0; }
>>>     void print( int a ){}
>>>     void print( double a ){}
>>> }
>>>
>>> class B{
>>>     private   A a;
>>>
>>>     alias a.getPrice getPrice;
>>>     /** Same like writing
>>>     int getPrice(){ return a.getPrice(); }
>>>     */
>>>
>>>     alias a.print print;
>>>     /** Same like writing
>>>     void print( int a ){ a.print( a ); }
>>>     void print( double a ){ a.print( a ); }
>>>     */
>>> }
>>>
>>> void main(){
>>>     B b = new B;
>>>     b.print( 1 );
>>> }
>>>
>>> The advantage is, changing the signature in A, automatically is visible
>>> for users of B. Less code, more consistency.
>>>
> 



More information about the Digitalmars-d mailing list