D casting broke?

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jun 19 13:18:14 PDT 2016


On Sunday, 19 June 2016 at 19:59:28 UTC, Joerg Joergonson wrote:
> This should be completely valid since B!T' obviously derives 
> from A!T directly and we see that T' derives from b which 
> derives from a directly. So B!b is an entirely derived from A!a 
> and hence the cast should be successful

I don't see how you think that. Here's the parent chain:

B!b -> A!b -> X -> x -> Object

There's no A!a in there, the cast is failing correctly.

Just because `b` is a child of `a` doesn't mean that `A!b` is the 
same as `A!a`. Consider an array:

MyClass[] arr;
arr ~= new MyClass(); // ok cool

Object[] obj_arr = arr; // won't work! because...
obj_arr[0] = new RandomClass(); // this would compile...

// but obj_arr and arr reference the same data, so now:
arr[0] is typed MyClass... but is actually RandomClass! It'd 
crash horribly.



Array is just one example of where converting A!b to A!a is 
problematic. The same principle can apply anywhere, so it won't 
implicitly cast them.



> The obviously question: Is there a simple way around this?

What are you actually trying to do?


More information about the Digitalmars-d-learn mailing list