Union redux

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Mon Jun 1 17:11:17 PDT 2015


On 6/1/15 3:43 PM, deadalnix wrote:
> On Monday, 1 June 2015 at 19:43:33 UTC, Andrei Alexandrescu wrote:
>> Do not define constructors. They ostensibly imply commitment to
>> remembering to call the appropriate destructor. A union can only be
>> default constructed (with its init value as described) and has no
>> destructor.
>>
>
> This is implied in C++ because of manual memory management. I don't
> think it is in D.

It is because destructors exist in both languages.

>>> 5/ union currently disallow members with postblit and/or destructor . It
>>> seems that this was needed in C++ as per Andrei's comments. It seems to
>>> me that, because of manual memory management, C++ would have a lot more
>>> struct with copy constructor and/or destructor than in D, so I'm not
>>> sure if this require change in spec. Andrei, can you give more details
>>> on the C++ situation here ?
>>
>> That rule has hurt C++ everywhere, and the community credits Lois
>> Goldthwaite for pushing the current rule, which allows types with
>> cdtors in unions. Let's go the same way, unions are all but useless
>> otherwise.
>>
>
> Without knowing the rationale in C++, it is difficult to assert that
> this is also valid in D. You seems to be knowledgeable of the subject,
> so surely you can enlight us on that one.

I intentionally gave enough information to allow easy further research. 
http://goo.gl/19Nol5 lists N2412 and N2248 as top hits, their motivation 
sections are relevant.

>>> 7/ struct with anonymous union in them have one entry per union field in
>>> their tupleof. All the union field should be present as one entry in the
>>> tupleof, with a compiler generated union type.
>>
>> Again the litmus test here is enabling introspection. Structs with
>> anonymous union members should have one member called e.g.
>> __anonymous__ in their tupleof, and in turn introspecting that member
>> should list its tuple.
>>
>
> Compiler generated tuples are auto flattening in D. That would not work
> unless the entry in the struct tuple is an (compiler generated) union.

So it seems like generating an __anonymous__ name is the way to go.


Andrei



More information about the Digitalmars-d mailing list