Sign in to follow this  

Length of an enum

This topic is 4340 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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];

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
"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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 ^_^

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4340 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this