digitalmars.D.bugs - DOC BUG: Enums and Static Initialization of Arrays
- John Reimer (26/26) Dec 11 2004 Not sure if I'm clear on this one:
-
Derek
(21/56)
Dec 11 2004
- John Reimer (20/49) Dec 11 2004 That's what I ended up doing in my own code... appending the +1. I
Not sure if I'm clear on this one: The digitalmars docs say that an enum X { A, B, C } will define A as 0 implicitly. It's equivalent to const int A = 0; const int B = 1; const int C = 2; The enum property max can be used to retrieve the enum's largest value such that X.max is equivalent to X.C (or 2); Let us go to the arrays section of the documentation now. Under array initialization, static intialization of static arrays, the docs give us an example of using an enumeration max property to set the size of an array. enum Color { red, blue, green }; int value[Color.max] = [ blue:6, green:2, red: 5 ]; How is this done? If Color.max == green == 2 (since Color.red in the enum starts at 0 by default), this means that value[] is declared with only 2 indexes, but the intialization tries to fill the value[] with 3. I assume the manual is wrong... or I've missed something obvious. What would be useful then is having a enum property that actually returns the number of enum members (not just max), something like Color.atoms or Color.elements (I'm not sure what a good name for that would be). Later, John
Dec 11 2004
On Sat, 11 Dec 2004 09:09:39 -0800, John Reimer wrote:Not sure if I'm clear on this one: The digitalmars docs say that an enum X { A, B, C } will define A as 0 implicitly. It's equivalent to const int A = 0; const int B = 1; const int C = 2; The enum property max can be used to retrieve the enum's largest value such that X.max is equivalent to X.C (or 2); Let us go to the arrays section of the documentation now. Under array initialization, static intialization of static arrays, the docs give us an example of using an enumeration max property to set the size of an array. enum Color { red, blue, green }; int value[Color.max] = [ blue:6, green:2, red: 5 ]; How is this done? If Color.max == green == 2 (since Color.red in the enum starts at 0 by default), this means that value[] is declared with only 2 indexes, but the intialization tries to fill the value[] with 3. I assume the manual is wrong... or I've missed something obvious.<code> import std.stdio; enum Color { red, blue, green }; void main() { static int value[cast(int)Color.max+1] = [ Color.blue:6, Color.green:2, Color.red: 5 ]; writefln("%d", cast(int)Color.max); } </code> In the example above, you need the "+1" otherwise the compiler says "Error: too many initializers 3 for array[2]" Also, the enum name qualifiers are required. The Docs need a bit of work here. It was curious that the writefln() call needed to have a cast too. I would have thought that Color.max would have been an int.What would be useful then is having a enum property that actually returns the number of enum members (not just max), something like Color.atoms or Color.elements (I'm not sure what a good name for that would be).Maybe Color.length ? -- Derek Melbourne, Australia
Dec 11 2004
Derek wrote:<code> import std.stdio; enum Color { red, blue, green }; void main() { static int value[cast(int)Color.max+1] = [ Color.blue:6, Color.green:2, Color.red: 5 ]; writefln("%d", cast(int)Color.max); } </code> In the example above, you need the "+1" otherwise the compiler says "Error: too many initializers 3 for array[2]" Also, the enum name qualifiers are required. The Docs need a bit of work here. It was curious that the writefln() call needed to have a cast too. I would have thought that Color.max would have been an int.That's what I ended up doing in my own code... appending the +1. I never tested the qualifier requirement. That's odd. I assumed that was just some eye candy. I liked the idea, but it never occured to me that it was required. Concerning the cast(int) requirement for Color.max to work in writefln(): perhaps it's due to the compiler "bug fix" in 0.107. Specifically, "Tightened up detection of constants being implicitly converted to a type that cannot hold it." -- 0.107 changelog. Now this may not seem completely logical... but I recall shortly after this fix, there were a huge number of casts that had to be added into D source code to make things compile. Since enums are equivalent to constants, perhaps the issue carries over to this situation as well. Does anyone know if the above example compiles without a cast on a previous dmd version?Hmmm... it's not quite a "length." But that's a possible alternative. Later, JohnWhat would be useful then is having a enum property that actually returns the number of enum members (not just max), something like Color.atoms or Color.elements (I'm not sure what a good name for that would be).Maybe Color.length ?
Dec 11 2004