[net.lang.c] Standard for union initializatio

jim@ISM780B.UUCP (01/23/85)

>I saw a suggestion in net.lang.c a few months ago that I found very sensible,
>readable and hygienic:
>
>        union   {
>                int i;
>                float x;
>                char *str;
>        } ixstr = { , 3.14, };
>
>if you want it to start off as a float, etc.
>
>Note that:
>1. it is (almost) isomorphous to a struct initialization,
>2. it also works nicely for arrays of unions.

Very very nice!  And if structure initialization is extended to allow
(internal, due to the trailing comma hack) empty initializers to yield the
default value (0), it is totally isomorphic.
I presume that `ixstr = {, 3.14}' is ok.
So the "first member rule" becomes the "<number of leading commas>+1'th
member rule".  Note that this is even compatible with the first member rule
that Whitesmith's already implements, so Plauger will have no reason to
quash it.  Now if the committee continues to stick with the first member
rule despite intelligent ideas like this, we should petition ANSI
for a new committee.

-- Jim Balter, INTERACTIVE Systems (ima!jim)

kpmartin@watmath.UUCP (Kevin Martin) (01/27/85)

>>I saw a suggestion in net.lang.c a few months ago that I found very sensible,
>>readable and hygienic:
>>
>>        union   {
>>                int i;
>>                float x;
>>                char *str;
>>        } ixstr = { , 3.14, };
>>
>>if you want it to start off as a float, etc.
>>
>>Note that:
>>1. it is (almost) isomorphous to a struct initialization,
>>2. it also works nicely for arrays of unions.
>
>Now if the committee continues to stick with the first member
>rule despite intelligent ideas like this, we should petition ANSI
>for a new committee.

"Intelligent idea"??? This is just another accident waiting to happen!
This looks ok for a nice short example like this, but frequently, the
union's definition and initialization are far apart (and maybe in different
source files). This makes it easy to add another union element, and
inadvertantly screw up the initializers royally without as much as a
peep from the compiler.

The first-element rule is a reasonable default, and there have been far
better suggestions than the above to select another element.
                    Kevin Martin, UofW Software Development Group.

augustss@chalmers.UUCP (Lennart Augustsson) (02/01/85)

In article <11143@watmath.UUCP> kpmartin@watmath.UUCP (Kevin Martin) writes:
>...
>>>
>>>        union   {
>>>                int i;
>>>                float x;
>>>                char *str;
>>>        } ixstr = { , 3.14, };
>>>
>>>if you want it to start off as a float, etc.
>>>
>>>Note that:
>>>1. it is (almost) isomorphous to a struct initialization,
>>>2. it also works nicely for arrays of unions.
>...
>"Intelligent idea"??? This is just another accident waiting to happen!
>This looks ok for a nice short example like this, but frequently, the
>union's definition and initialization are far apart (and maybe in different
>source files). This makes it easy to add another union element, and
>inadvertantly screw up the initializers royally without as much as a
>peep from the compiler.
>
>The first-element rule is a reasonable default, and there have been far
>better suggestions than the above to select another element.
>                    Kevin Martin, UofW Software Development Group.

Doesn't the same argument hold for structures too?  Do you want to change
the initialization of them?  I think the original suggestion is quite
reasonable.

-- 
Any resemblance of the opinions above to real opinions, living or dead,
is purely coincidential.
		Lennart Augustsson
		{seismo,philabs,decvax}!mcvax!enea!chalmers!augustss