Upcasting slice of class elements

Adam D. Ruppe destructionator at gmail.com
Fri Jan 5 22:24:43 UTC 2018


On Friday, 5 January 2018 at 22:16:04 UTC, Nordlöw wrote:
> Why isn't
>
>     class X {}
>     class Y : X {}
>     X[] xs = cast(X[])(Y[].init);
>
> compilable in safe D?
>
> What's unsafe about such a cast?

class X {}
class Y : X {}
class Z : X {}

Y[] ys = Y[].init;
X[] xs = cast(X[])(ys);
xs[0] = new Z;


What happens to ys[0] there?


More information about the Digitalmars-d-learn mailing list