equivariant functions

Christopher Wright dhasenan at gmail.com
Sun Oct 12 16:27:29 PDT 2008


Andrei Alexandrescu wrote:
> Christopher Wright wrote:
>> All your examples use typeof(something). Is the intent to make this 
>> example work?
>>  > class A { A clone(); }
>>  > class B : A { B clone(); }
> 
> That example does work today. It's covariance of return values and is 
> only loosely related to this topic.
> 
>> I take it that this would also extend to delegates?
>>
>> class A {}
>> class B : A {}
>>
>> void foo (A delegate (A) dg) { }
>> B bar (B b) { }
>> foo (&bar);
>>
>> If you require "typeof(something)", I'd never use this feature. 
>> Otherwise, I'd use it sometimes.
> 
> The example is wrong because B delegate(B) is not a supertype nor a 
> subtype of A delegate(A). The proposed solution does involve writing 
> typeof as an indication of the need to return the same type as the 
> argument's.
> 
> 
> Andrei

My mistake -- I could have written it as:
void foo (A delegate (B) dg) { }
B bar (A a) { }
foo (&bar);

No input that foo can give to the delegate cannot be given to bar, and 
no output from bar can be unacceptable.



More information about the Digitalmars-d mailing list