enums

Alexis Layton alex at cca.CCA.COM
Thu Jul 28 07:12:06 AEST 1988


To jump into this fray, I think I'd have to agree with Henry.  Enums are
completely superfluous if they do not provide both type-checking and better
storage control.  I would like to see enum have the following type-checking
properties:  convertible to and from ints automatically, but only assignable
to enums of the same type.  This is very much like the relationship between
pointers and pointers to void.

	enum able { Alpha, Beta };
	enum baker { Red, Green };

	enum able a;
	enum baker b;
	int i;

	a = Alpha;	/* ok */
	b = Green;	/* ok */
	a = Green;	/* oops: wrong type */
	a = b;		/* ditto */
	a = 6;		/* ok, ints convertible to enums */
	i = a;		/* ok, enums convertible to ints */
	b = i;		/* also ok */
	i = Green;	/* ok */

Actually, it may be better to restrict int->enum; so that "a = 6" above
would fail without explicit cast.  But enum->int must be allowed (which
the pcc does not), so you can have

	char *baker_names[] = { "Red", "Green" };

and say

	printf("%s\n", enum_names[b]);

A second useful thing with enums would be if the "short" and "long" adjectives
would apply to them.  (You'd also like a "char"-size one, but "char" is not
an adjective; don't know just what to do about this, maybe "byte"?)
Then you could easily specify smaller enumeration sizes without using
bitfields.

Well, it's probably too late now for the ANSII standard; but maybe on the
next go-around (you know, NEXT year.... :-) ) this might be looked into.

Alexis Layton
alex at CCA.CCA.COM



More information about the Comp.lang.c mailing list