[Nickle]New 'enum' union member type
Keith Packard
keithp@keithp.com
Tue, 14 Aug 2001 01:42:46 -0700
Borrowing from ML, Bart and I added a new union member 'type' called
'enum'. This type is the same as void, except that the constructor takes
no argument (it doesn't need one in any case, 'void' has but a single
value).
Also changed is the syntax for calling the union constructor, instead of
the strange cast-like syntax:
(<type> . <name>) value
we now have:
<type> . <name> (value)
for regular union members. For the new 'enum' members, we've got:
<type> . <name>
They're called 'enum' because you can treat them a lot like an enumerated
type:
typedef union { int a; enum empty; enum broken; } foo;
foo x;
x = foo.a (10); /* regular constructor */
x = foo.empty; /* enum constructor */
union switch (x) {
case a:
printf ("a %d\n", x.a);
break;
case empty:
printf ("empty\n");
break;
case broken:
printf("broken\n");
break;
}
For syntactic sugar, you can create a union full of only enums with:
typedef enum { red, green, blue, yellow } colors;
Now you can do things like:
colors c;
c = colors.red;
if (c == colors.yellow) printf ("yellow\n");
Of course, you can still use the union switch:
union switch (c) {
case red:
case green:
case blue:
case yellow:
}
Perhaps this final case should be changed to 'enum switch' instead?
-keith