covariance, operator overloads, and interfaces

Michel Fortin michel.fortin at michelf.com
Tue May 11 07:40:22 PDT 2010


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.

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/



More information about the Digitalmars-d mailing list