Container Contravariance was Re: [typing] Type-erasure re generics

Jesse Phillips jessekphillips+D at gmail.com
Wed Sep 29 13:37:49 PDT 2010


Jonathan M Davis Wrote:

> Pelle's point still stands.
> 
> a[0] = new A();
> 
> would be legal code if you could assign a B[] to an A[], and since an A isn't 
> necessarily a B, that doesn't work at all. If the underlying type were actually 
> A[], then it would be fine to use varying types derived from A, but since the 
> underlying type would actually be B[], it would break B[] to put anything in it 
> which wasn't a B or a type derived from B.

Ok, then let us actually test that and show it is valid[1]:

class A {}
class B:A {}
 
void main() {
    A[] a = new B[6];
    a[0] = new A();
}

1. http://ideone.com/5sUZt

I have shown that what I am talking about is valid for Arrays (a container) but not templates. The issues of actually implementing it is not related to what I was replying to. I introduced it as a benefit to forgetting type information in "generics." I was then told "Having a container of one type should not be castable to a container of
another type." So I am pointing out we need to remove this feature from arrays right now!


More information about the Digitalmars-d mailing list