covariance, operator overloads, and interfaces
    Steven Schveighoffer 
    schveiguy at yahoo.com
       
    Tue May 11 07:32:54 PDT 2010
    
    
  
On Tue, 11 May 2010 09:33:38 -0400, Steven Schveighoffer  
<schveiguy at yahoo.com> wrote:
> I don't know how to solve this without dropping interfaces, or repeating  
> the template in all derived classes.
Here's an idea.
What if when using an auto return on a template, it becomes automatically  
covariant for derived classes?
For example:
interface List
{
    List append(List l);
    auto opOpAssign(string op : "~=")(List rhs)
    {
        return append(l);
    }
}
class ArrayList : List
{
    ArrayList append(List l);
}
ArrayList al = new ArrayList;
ArrayList al2 = new ArrayList;
auto x = (al ~= al2);
when compiling the above line, the compiler should recognize that the  
template returns auto.  It does the calculation of what type to return  
based on the context.  In the context of ArrayList, append returns  
ArrayList, so opOpAssign should return ArrayList.
Problems may arise if the compiler can easily determine the result when  
dealing with straight interfaces, but cannot easily determine when dealing  
with derived classes.  I think in those cases, the compiler should just  
output an error, and the user will be forced to concretely specify the  
return type, or fix their method to be more covariant-friendly.
How does this sound?
I'd also like to see this for final interface functions as well.
-Steve
    
    
More information about the Digitalmars-d
mailing list