covariance, operator overloads, and interfaces

Steven Schveighoffer schveiguy at
Tue May 11 06:33:38 PDT 2010

The situation with interfaces and operators is getting worse and worse as  
I try to implement it.

Here is another issue I have found.

Previously in dcollections, I support the ~ operator on Lists.

The relevant interface looked something like this:

interface List
    List opCat(List rhs);

So something like an array list would use covariance to make the syntax  
sugar more pleasant:

class ArrayList : List
    ArrayList opCat(List rhs);

Now, you can do things like this:

ArrayList al, al2;

al = al ~ al2;

Fine and dandy.  However, with the new operator overloading scheme, I must  
define the operator as follows:

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.


More information about the Digitalmars-d mailing list