Union literals? (D1.0)

Jarrett Billingsley kb3ctd2 at yahoo.com
Thu Feb 7 14:16:46 PST 2008


"Leandro Lucarella" <llucax at gmail.com> wrote in message 
news:20080207212438.GB16062 at burns.springfield.home...

> Is there any way to initialize a C in one line (so it can be const)? Like:
>
> const c = C(10, C.U.D(B('b'), 15));

Since it's const, you can use the oh-so-special struct initializer syntax:

const C c = { y: 10, u: { d: { b: B('b'), domain: 15}}};

But this is getting a little ugly.

I'm not sure if the struct you posted was stripped down from a larger 
struct, or if you're just unaware of the fact that D has anonymous structs 
and unions.  C can be declared more simply as:

struct C
{
    int y;

    union
    {
        A a;

        struct
        {
            B b;
            uint domain;
        }
    }
}

And then initialized:

const C c = { y: 10, b: B('b'), domain: 15 };

Also any instances of C you create, you no longer have to write "c.u.d.b" 
etc., just "c.b". 




More information about the Digitalmars-d-learn mailing list