[typing] Type-erasure re generics

Jonathan M Davis jmdavisProg at gmx.com
Wed Sep 29 13:13:00 PDT 2010


On Wednesday, September 29, 2010 12:36:02 Steven Schveighoffer wrote:
 > In such a case, would would it even _mean_
> > to try and assign one container type to another? Sure, if they're
> > classes, and
> > they share the same API, you may be able to get it to work thanks to the
> > fact
> > that you're dealing with references.
> 
> No, even with that, you brought it up earlier -- class C could say:
> 
> static if(is(T == A))
>     void anotherfunction() {}
> 
> and then the vtable for C!B is different than C!A, even though the vtable
> for B is compatible with the vtable for A.
> 
> There has to be some way for the compiler to determine this, but again,
> only if this feature makes sense to implement.

I forgot about the vtable. That makes things even harder.

Overall, it looks like you'd have to really work at it t omake it possible and 
that even then it would only work under limited circumstances, and those 
circumstances may not even be particularly apparent to the programmer. So, it 
would probably be a very difficult feature to understand, let alone use, because 
it wouldn't be at all clear when it would work. And to make things worse, all it 
would take would be one small change in the container code which broke the 
binary compatability and all your code that relied on it working with that type 
would break.

So, it may be theoretically possible to get some version container compatability 
based on the inheritance hiercharcy of their elements to work, but it would be 
hard to implement, likely difficult to use, and easy to break.

It's just easier to cope with containers not being assignable to one another 
just because the type of the elements of one is derived from the type of the 
elements of the other.

- Jonathan M Davis


More information about the Digitalmars-d mailing list