equivariant functions

Aarti_pl aarti at interia.pl
Mon Oct 13 06:45:57 PDT 2008


Andrei Alexandrescu pisze:

>>> In unrelated proposals and discussions, people mentioned the need for 
>>> functions that return the exact type of this:
>>>
>>> class A { A clone(); }
>>> class B : A { B clone(); }
>>>
>>> How can we declare A.clone such that all of its derived classes have 
>>> it return their own type?
>>
>> In this case I would just use typeof(this)
>>
>> class A { typeof(this) clone(); }
>> class B : A { typeof(this) clone(); }
>>
>> I don't see a reason why it should be same as in above case.
> 
> Because you don't want B's implementor to implement clone to return an 
> A. That would be a mistake.
> 

IMHO it is much more consistent than current situation. When you write 
typeof(this) in class B it should mean B not A (currently when 
overriding A function with above signature you will get result of type A 
), because "this" for B objects is clearly of type B. Additionally such 
a syntax shows that function participate in overriding, as the result 
type is same (like in common cases).

BTW. I hope that Walter will reconsider more complicated solutions (also 
my proposal :-) ) because compiler "facade" to user is very important. 
This is the syntax, which makes working with language pleasure or 
difficult. It might be worthy to put more time into implementation, but 
get more flexible and clean solution.

BR
Marcin Kuszczak
(aarti_pl)



More information about the Digitalmars-d mailing list