covariance, operator overloads, and interfaces

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Tue May 11 07:51:01 PDT 2010


Michel Fortin wrote:
> On 2010-05-11 09:33:38 -0400, "Steven Schveighoffer" 
> <schveiguy at yahoo.com> said:
> 
>> interface List
>> {
>>     List concat(List rhs);
>>     List opBinary(string op)(List rhs) if (op == "~")
>>     {
>>         return concat(rhs);
>>     }
>> }
>>
>> But now I lose covariance, the above usage no longer compiles.
>>
>> I don't know how to solve this without dropping interfaces, or 
>> repeating  the template in all derived classes.
> 
> Can't you repeat the opBinary template in the ArrayList class with a 
> covariant return?
> 
> class ArrayList : List {
>     ArrayList concat(List rhs);
>     ArrayList opBinary(string op)(List rhs) if (op == "~") {
>         return concat(rhs);
>     }
> }
> 
> Not very elegant, but I think it'll work.

He already mentioned "repeating the template in all derived classes".

This is another place in which we need typeof(class) to yield the class 
being currently compiled.


Andrei


More information about the Digitalmars-d mailing list