Length of an enum

Started by
8 comments, last by Zahlman 18 years, 2 months ago
Is there a way to find the number of members an enum has? I would prefer not to just use the name of the last member (plus one) and rely on that not changing. Let me be more specific : I have an enum
enum VARTYPES {mass, momentum, spin}
and I would like to declare an array
double myVars[3];
without resorting to the hardcoded 3, (spin+1), or anything else that relies on the VARTYPES enum not changing.
To win one hundred victories in one hundred battles is not the acme of skill. To subdue the enemy without fighting is the acme of skill.
Advertisement
it is common to see an enum with one extra value, the size

eg:
enum VARTYPES {mass, momentum, spin,MAX_VARTYPES}

this doesnt work too well when values are assigned to the enums, but will work fine for you, provided you remember not to add any types after that one


double myVars[MAX_VARTYPES];
There is no way I'm afraid. =(
Ah well, my approach to the problem wasn't general enough anyway. Bit of premature optimisation there, I think. :)
To win one hundred victories in one hundred battles is not the acme of skill. To subdue the enemy without fighting is the acme of skill.
That's one of the nicer little features of Pascal/Delphi - the way you can declare a type that's an array subscripted with an enum. You'd just declare

double myVars[VARTYPES];

actually you'd declare

myVars: array[VARTYPES] of double;

but you get the point. The array is subscripted by VARTYPES. I'm kinda suprised that no other language has duplicated this convenience.
Quote:Original post by dalep
That's one of the nicer little features of Pascal/Delphi - the way you can declare a type that's an array subscripted with an enum. You'd just declare

double myVars[VARTYPES];

actually you'd declare

myVars: array[VARTYPES] of double;

but you get the point. The array is subscripted by VARTYPES. I'm kinda suprised that no other language has duplicated this convenience.


wrong:
enum{FIRST,SECOND,THIRD,LAST};int array[LAST];array[FIRST] = 1;array[SECOND] = 2;
"LAST", my dear rip-off, is not a type the way VARTYPES is. Its a value in an enum. Which is not quite the same thing. A Pascal type-subscripted array does not require dummy entries like LAST. Its a minor thing.
Quote:Original post by dalep
"LAST", my dear rip-off, is not a type the way VARTYPES is. Its a value in an enum. Which is not quite the same thing. A Pascal type-subscripted array does not require dummy entries like LAST. Its a minor thing.


i see. i am not familiar with pascal. however, your statement
Quote:
The array is subscripted by VARTYPES. I'm kinda suprised that no other language has duplicated this convenience.


seemed to imply that other languages couldn't use the enum as a subscript. sorry for the misunderstanding
Like you say, its really simple to throw a dummy at the end of an enum, which results in almost the same thing. Its just a tiny bit less typing in Pascal - you iterate for i = Low(typename) to High(typename) and so if your enum ranges from 1435 to 1466 it'll all still work with no dummy elements anywhere.

As I said, its a tiny savings of typing but its this sort of detail that makes the difference between languages. Nobody has invented a new kind of loop in a long time, afterall. Of course, in the case of Pascal you pay for that savings by having to type "procedure", "function", "then", "begin" and "end" hundreds upon hundreds of times. oh well ^_^
Quote:Original post by dalep
"procedure", "function", "then", "begin" and "end" hundreds upon hundreds of times. oh well ^_^


Random musing: Larry Wall (inventor of Perl) has often been quoted talking about the "huffman coding of keywords", i.e. the idea that language keywords that are used more often should be shorter than the ones used less often. Python takes this to heart pretty well, getting rid of then/begin/end entirely, not even bothering with the { and } that replace them in C-like languages. But they have this cryptic keyword to introduce functions: def (presumably short for "definition"). I do agree that it's a good idea to have some keyword for this purpose - it helps distinguish declarations from invocations, and thus lets the compiler/interpreter tell you more about syntax errors. But I think we can do better - shorter still, and yet "reading" properly.

I propose: to.

This topic is closed to new replies.

Advertisement