how to use opdot

Ary Borenszweig ary at esperanto.org.ar
Fri Nov 21 05:36:14 PST 2008


Christopher Wright escribió:
> Steven Schveighoffer wrote:
>> "Christopher Wright" wrote
>>> Steven Schveighoffer wrote:
>>>> Not exactly ;)  The wrapped type is not equivalent to inheritance.
>>> No, exactly:
>>>
>>> class Wrapper(T) : T
>>> {
>>> T opDot() {}
>>> }
>>
>> class Wrapper(T) : T
>> {
>> }
>>
>> works just as good ;)
>>
>> -Steve 
> 
> True, but with opDot, you can swap out the real value. I can think of 
> cases in which this would be useful -- a sort of "I'll fill in this 
> value later" thing.

I'm not sure that's the decorator pattern any more. In that pattern you 
implement or extend a class, and receive an instance of one in the 
constructor and forward all calls to that instance. If you do:

class Wrapper(T) : T {

   private wrapped;

   this(T wrapped) {
     this.wrapped = wrapped;
   }

   T opDot() { return wrapped; }

}

that won't work because whenever you call a method of T on Wrapper(T), 
that will call Wrapper's method, since it has it, because it 
extends/implements T (opDot won't be triggered). If you remove 
inheritance, that will probably work, but you won't be able to make a 
Wrapper(T) behave like a T for the type system.


More information about the Digitalmars-d-learn mailing list