Euphoria
Ticket #432:
enum type give incorrect result
-
Reported by
coconut
Nov 22, 2010
try this
enum type color RED=4, GREEN=7, BLACK=1, BLUE=3 , PINK=10 end type
? color(RED) --> 1
? color(GREEN) --> 2
? color(BLACK) --> 3
? color(BLUE) --> 4
? color(PINK) --> 5
now this
enum color RED, GREEN=7, BLACK=1, BLUE=3, PINK=10 end type
? color(RED) --> 1
? color(GREEN) --> 2
? color(BLACK) --> 1
? color(BLUE) --> 4
? color(PINK) --> 5
Details
1. Comment by jeremy
Nov 22, 2010
Here is another example and an additional enum problem, i.e. color2(RED) has a value but was never declared? I have not looked into the enum type code so I'm not sure the proper response here.
enum type color1 RED=4, GREEN=7, BLACK=1, BLUE=3, PINK=10 end type
enum type color2 RED1, GREEN1=7, BLACK1=1, BLUE1=3, PINK1=10 end type
? { RED, GREEN, BLACK, BLUE, PINK }
? { RED1, GREEN1, BLACK1, BLUE1, PINK1 }
? { color1(RED), color1(GREEN), color1(BLACK), color1(BLUE), color1(PINK) }
? { color2(RED), color2(GREEN), color2(BLACK), color2(BLUE), color2(PINK) }
? { color2(RED1), color2(GREEN1), color2(BLACK1), color2(BLUE1), color2(PINK1) }
2. Comment by mattlewis
Nov 23, 2010
I guess the hacked up type routine is returning the result of find instead of 1 or zero.
3. Comment by DerekParnell
Nov 23, 2010
Firstly, I hope that you realize that an enum type function returns an integer from 0 to the count of enum values defined for the type. This is different from regular type functions, which only return 0 or 1. An enum type function returns the ordinal value of the argument within the enum type.
Thus
enum type color1 RED=4, GREEN=7, BLACK=1, BLUE=3, PINK=10 end type
has five enum defined in the type so its type function can return an integer in the range 0 to 5. RED returns 1 because it is the first value, GREEN returns 2 becuase it is the second vlaue, etc ... 0 is returned when the argument is not in the enum type.
Jeremy, the call to color2(RED) returns 0, which is the correct value to return. A zero returned from a enum type function means that the value used as the argument was not defined within that enum.
As for the original reported error, you have defined both RED and BLACK to be the value 1, so supplying either to the enum type function is going to return the first enum that matches the type function's argument. You can think of it like this...
The enum called 'color' has a set defined as {1, 7, 1, 3, 10} representing by the names RED, GREEN, BLACK, BLUE and PINK respectively. So when you call the type function color with the argument BLACK, Euphoria looks for the first BLACK (ie. 1) that is in the defined set for the enum. This is the first element so it returns 1.
Anyhow, I don't think we have a bug here at all as it is working as documented and as I expected it to.
4. Comment by jeremy
Nov 23, 2010
So it's just a lack of understanding. Marking as invalid.
5. Comment by coconut
Nov 23, 2010
Hum! the doc says
"The value returned is the ordinal number of the member in the enum's definition, regardless of what the member's value is"
So this is really a bug! because in the example I gave this should print 1 2 3 4 5 in both cases.
-- This agree with what I read in the doc
enum type color RED=4, GREEN=7, BLACK=1, BLUE=3 , PINK=10 end type
? color(RED) --> 1
? color(GREEN) --> 2
? color(BLACK) --> 3
? color(BLUE) --> 4
? color(PINK) --> 5
-- this doesn't agree with the doc. It should print 1 2 3 4 5 too.
enum type color RED, GREEN=7, BLACK=1, BLUE=3 , PINK=10 end type
? color(RED) --> 1
? color(GREEN) --> 2
? color(BLACK) --> 1
? color(BLUE) --> 4
? color(PINK) --> 5
</eucoode>
6. Comment by coconut
Nov 23, 2010
Ok! after some experimentation I understand, it does a find on the set and return the position. In the second example I gives, RED and BLACK have both value 1. So the set is {1,7,1,3,10} so color(RED) is found at position 1 and color(BLACK) is found at position 1 too. It's not a bug but that precision should be added in the doc: WARNING: don't put two identicals values implicitly or explicitly in a type enum.
7. Comment by DerekParnell
Nov 23, 2010
Fair enough, the documentation could be updated to mention what happens when the coder chooses to have alias enum names.
8. Comment by jimcbrown
Nov 23, 2010
Documentation updated with this exception in svn:4281