November 21, 2008 Re: how to use opdot | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | 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.
|
November 21, 2008 Re: how to use opdot | ||||
---|---|---|---|---|
| ||||
Posted in reply to Christopher Wright | 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.
|
November 21, 2008 Re: how to use opdot | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ary Borenszweig | "Ary Borenszweig" wrote
> 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.
Exactly what I was going to say ;) If you inherit from a T, then return T in an opDot, opDot will never be called unless you call opDot directly.
-Steve
|
Copyright © 1999-2021 by the D Language Foundation