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

Jonathan M Davis jmdavisProg at gmx.com
Wed Sep 29 15:23:04 PDT 2010


On Wednesday, September 29, 2010 13:37:49 Jesse Phillips wrote:
> 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!

In Java, you can't use generics with arrays, so it's not an issue.

But if the D code you give here compiles, then that's a bug and needs to be 
reported.

- Jonathan M Davis


More information about the Digitalmars-d mailing list