[net.lang.c] Enums as Indices

cottrell@NBS-VMS.ARPA (COTTRELL, JAMES) (02/19/86)

/*
> ---though to my mind using enumerated types as indicies is `treading
> on thin ice' (I have no explanation as to why I feel this way; I
> just do).
> -- 
> In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1415)

Perhaps I can allay some of your doubts. First, remember that in AWK
subscripts may be string valued. Thus: `/apple/ { x["apple"]++ }' is OK.
Second, I feel that ENUM's are merely a way of writing #defines with a
bit of type checking thrown in (thus the error). One of the things you
can do with integers is use them for subscripts. I can imagine a
statement like `wavelenth[RED] = however_many_angstroms_red_really_is'.
This moves us closer to a database view of things where the sparse
matrix of things like 'RED', 'BLUE', etc., are mapped into small integers.
An expression such as `wavelength['RED'] = ...' is currently legal altho
not recommended (portability). I am reminded of LISP's property lists, 
which are conceptually `indices of arbitrary type.' Another thought
that comes to mind is that struxure members are somewhat enumerated
offset constants for a particular struxure. Remember how they used
to do struxures in FORTRASH? Oh, well, I'm rambling.

	jim		cottrell@nbs
*/
------

guy@sun.uucp (Guy Harris) (02/20/86)

> Second, I feel that ENUM's are merely a way of writing #defines with a
> bit of type checking thrown in (thus the error). One of the things you
> can do with integers is use them for subscripts. I can imagine a
> statement like `wavelenth[RED] = however_many_angstroms_red_really_is'. ...
> An expression such as `wavelength['RED'] = ...' is currently legal altho
> not recommended (portability).

Unfortunately, as pointed out such an expression is not legal according to
PCC, at least, although ANSI C indicates that "enum"s are really just funny
names for "int"s and presumably allows their use as subscripts.  (This means
that the type checking may go away, although it'd be nice to have it at
least issue a warning on a type clash; consider the definition

	typedef enum { READ, WRITE } op_type;
	typedef enum { BYTES, CHARACTERS } io_mode;
	int funny_io(int, char *, int, op_type, io_mode);

which could catch errors like

	ntransferred = funny_io(fd, buf, count, BYTES, READ);

)  Conceptually, an array of type "x" using type "y" as a subscript is a
function from the set of members of type "x" to the members of type "y".
However, C doesn't permit you to specify "y" directly as a type, so
you can't do

	enum color { red, orange, yellow, green, blue, indigo, violet };
	float wavelength[enum color];

to indicate that the valid subscripts for "wavelength" are members of the
enumeration type "enum color" and to tell it to allocate 7 elements for the
array.  Things would get even messier if you defined a sparse enumeration;
should it allocate array elements for all the missing elements, or implement
the array differently from a dense one?  Furthermore, to make arrays indexed
by a subset of the integers work, you'd have to add subrange types to C;
this might be a nice thing to do, but it drags in a lot of additional
baggage.
-- 
	Guy Harris
	{ihnp4, decvax, seismo, decwrl, ...}!sun!guy
	guy@sun.arpa	(yes, really)