built-in types inside of a union

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jul 13 07:01:25 PDT 2016


On Wednesday, 13 July 2016 at 12:28:57 UTC, zodd wrote:
> This code works as I expected but I'm unsure that it's correct.

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.


More information about the Digitalmars-d-learn mailing list