built-in types inside of a union

zodd via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jul 13 08:16:50 PDT 2016


On Wednesday, 13 July 2016 at 14:01:25 UTC, Adam D. Ruppe wrote:
> Yes, your code is legal.
>
> What isn't legal is using some type that isn't there at 
> runtime, like
>
> union A {
>    int[] a;
>    char[] b;
> }
>
> A u;
>
> u.a = [1,2];
>
> u.b.length
>
>
> The compiler will let you do it, but being a union, it will 
> show length == 1 because it was set to an int[], but that's not 
> really correct for a char[].
>
>
> But the way you did it, initializing to the proper type before 
> accessing it, is allowed.

Thank you!


More information about the Digitalmars-d-learn mailing list