www.digitalmars.com         C & C++   DMDScript  

D - ability to specify step for enums

reply "DrakeX" <kb3ctd2 yahoo.com> writes:
the problem with enums is that they only go by steps of 1.  while this is
fine in many cases, if you want to count by other numbers, you have to type:

enum values{ val1=2, val2=4, val3=6, val4=8 };

which pretty much defeats the purpose, as if you want to add a flag in
between two other flags, you'd have to renumber all the following flags.

it'd be nice if we could do something like

enum flags{ 2; 2; val1,val2,val3,val4 }

notice the semicolons.  the first parameter would be the start; the second
would be the step (you could also have -5 or whatever) and then the list of
values.

i know this is sort of bordering on preprocessor territory, but it'd be a
nice addition.
Sep 26 2003
next sibling parent "Matthew Wilson" <matthew stlsoft.org> writes:
I think this is a great idea. (I'm one of the one's who's not completely
glad to have seen the back of the pre-processor, so perhaps it's not good to
have me on your side.;)

"DrakeX" <kb3ctd2 yahoo.com> wrote in message
news:bl2qq1$4bg$1 digitaldaemon.com...
 the problem with enums is that they only go by steps of 1.  while this is
 fine in many cases, if you want to count by other numbers, you have to
type:
 enum values{ val1=2, val2=4, val3=6, val4=8 };

 which pretty much defeats the purpose, as if you want to add a flag in
 between two other flags, you'd have to renumber all the following flags.

 it'd be nice if we could do something like

 enum flags{ 2; 2; val1,val2,val3,val4 }

 notice the semicolons.  the first parameter would be the start; the second
 would be the step (you could also have -5 or whatever) and then the list
of
 values.

 i know this is sort of bordering on preprocessor territory, but it'd be a
 nice addition.
Sep 26 2003
prev sibling next sibling parent reply Benji Smith <dlanguage xxagg.com> writes:
In article <bl2qq1$4bg$1 digitaldaemon.com>, DrakeX says...
enum flags{ 2; 2; val1,val2,val3,val4 }
I like this idea. But I'd also like the ability to create increments that aren't necessarily arithmetic (like maybe geometric). For example, when creating bitwise flags, it could be nice to have the following:\ enum flags{ val1=1,val2=2,val3=4,val4=8,val5=16,val6=32 } So, using a format similar to the one you suggested, you might end up with:
enum flags{ 2; ^2; val1,val2,val3,val4,val5,val6 }
Yeah, I know that it would be difficult to parse, and it really shouldn't be implemented like this, but CONCEPTUALLY, it would be neat.
Sep 27 2003
parent reply Ant <Ant_member pathlink.com> writes:
In article <bl5pi7$10fq$1 digitaldaemon.com>, Benji Smith says...
In article <bl2qq1$4bg$1 digitaldaemon.com>, DrakeX says...
enum flags{ 2; 2; val1,val2,val3,val4 }
I like this idea. But I'd also like the ability to create increments that aren't necessarily arithmetic (like maybe geometric). For example, when creating bitwise flags, it could be nice to have the following:\ enum flags{ val1=1,val2=2,val3=4,val4=8,val5=16,val6=32 } So, using a format similar to the one you suggested, you might end up with:
enum flags{ 2; ^2; val1,val2,val3,val4,val5,val6 }
Yeah, I know that it would be difficult to parse, and it really shouldn't be implemented like this, but CONCEPTUALLY, it would be neat.
of course we have this: const long start = 2; const long step = 3; enum T : long { a = start, b = a + step, c = b + step, d = c*b, e = (d / step + c) * (d / step + c) , f = 143, g = f + step, }; just a reminder to people passing by casually through the D group, I know you know that. Ant
Sep 27 2003
parent "DrakeX" <kb3ctd2 yahoo.com> writes:
cool :)

 of course we have this:

 const long start = 2;
 const long step = 3;
 enum T : long
 {
 a = start,
 b = a + step,
 c = b + step,
 d = c*b,
 e = (d / step + c) * (d / step + c) ,
 f = 143,
 g = f + step,
 };
Oct 01 2003
prev sibling next sibling parent reply "Walter" <walter digitalmars.com> writes:
"DrakeX" <kb3ctd2 yahoo.com> wrote in message
news:bl2qq1$4bg$1 digitaldaemon.com...
 the problem with enums is that they only go by steps of 1.  while this is
 fine in many cases, if you want to count by other numbers, you have to
type:
 enum values{ val1=2, val2=4, val3=6, val4=8 };

 which pretty much defeats the purpose, as if you want to add a flag in
 between two other flags, you'd have to renumber all the following flags.

 it'd be nice if we could do something like

 enum flags{ 2; 2; val1,val2,val3,val4 }

 notice the semicolons.  the first parameter would be the start; the second
 would be the step (you could also have -5 or whatever) and then the list
of
 values.
You can write it like: enum values { val1 = 2, val2 = val1 * 2, val3 = val2 * 2, val4 = val3 * 2 };
Sep 28 2003
parent "DrakeX" <kb3ctd2 yahoo.com> writes:
never would've thought of that!  kind of like ant's method.

 You can write it like:
     enum values {
             val1 = 2,
             val2 = val1 * 2,
             val3 = val2 * 2,
             val4 = val3 * 2 };
Oct 01 2003
prev sibling next sibling parent Charles Hixson <charleshixsn earthlink.net> writes:
DrakeX wrote:
 ...
 i know this is sort of bordering on preprocessor territory, but it'd be a
 nice addition.
 
 
What it's stepping towards is a metalanguage. A language for rewriting the syntax of the language. Not a totally bad idea, but there a lots of bad precedents that show it can be done very wrongly. I keep being drawn not towards this idea, but towards it's opposite (as exemplified by languages like Python and Ruby). It keeps feeling like a great idea to have a language that in some unified way handles variables whose types aren't known until run time and with the same (almost) syntax handles variables determinable at compile time. And, of course, does each part efficiently. But I've never seen a good example. The closest I've seen to this is PyRex, which is a sort of interface between Python and C that lets you go back and forth without too much overhead (or so it is claimed). But notice how different the syntaxes are. Now the metalanguage kind of thing that you seem to be stepping towards has actually been done. But it was in a very simple language (FORTH). And it was, indeed quite powerful. But could also be nearly incomprehensible. This is probably also like LISP macros, but I'm not familiar enough about them to comment.
Sep 30 2003
prev sibling parent Ilya Minkov <minkov cs.tum.edu> writes:
I'd rather say: why not allow to initialise one enum from another by 
using a compile-time evaluatable expression? Thus you could:

  - multiply an enum by an integer, member-wise;
  - add an integer to an enum, member-wise;
  - ... all other operators you imagine;
  - concatenate 2 enums;
  - apply simple integer functions, member-wise;
  - apply simply array processing functions to a whole enum...

Obviously, this would requiere something like an interpreter. And an 
ability to define anonymous enums would be also neat.

How do you like it?

-eye

DrakeX wrote:
 the problem with enums is that they only go by steps of 1.  while this is
 fine in many cases, if you want to count by other numbers, you have to type:
 
 enum values{ val1=2, val2=4, val3=6, val4=8 };
 
 which pretty much defeats the purpose, as if you want to add a flag in
 between two other flags, you'd have to renumber all the following flags.
 
 it'd be nice if we could do something like
 
 enum flags{ 2; 2; val1,val2,val3,val4 }
 
 notice the semicolons.  the first parameter would be the start; the second
 would be the step (you could also have -5 or whatever) and then the list of
 values.
 
 i know this is sort of bordering on preprocessor territory, but it'd be a
 nice addition.
 
 
Oct 26 2003