contravariant argument types: wanna?

Steven Schveighoffer schveiguy at yahoo.com
Tue Sep 22 20:25:30 PDT 2009


On Tue, 22 Sep 2009 22:58:24 -0400, Andrei Alexandrescu  
<SeeWebsiteForEmail at erdani.org> wrote:

> Practically, there may be cases in which the derived class wants to make  
> clear that it only needs a more general parameter type. Because of that,  
> you'd be able to issue calls that you otherwise can't. Consider:
>
> class A { void fun(Y); }
> static if (contravariance)
>      class B : A { override void fun(X); }
> else
>      class B : A { override void fun(Y); }
> class X { }
> class Y : X { }
> class Z : X { }
>
> If what you have is a B and a Z, there is absolutely no way you could  
> make the call B.fun(Z) without contravariance. Z is unrelated to Y and  
> therefore casting it to a Y would throw.
>
> Now the only issue is giving good names for A, B, X, Y, and Z :o).

Ah, yes, I did not think of the case of a tree vs. a line :)

this is a better example than your original...

-Steve



More information about the Digitalmars-d mailing list