1. explain enum type to me
- Posted by _tom (admin) Oct 15, 2014
- 1357 views
explain enum type to me
Enum type is:
enum type rgb red, green, blue end type ? red ? green ? blue -- 1 -- 2 -- 3
The syntax is not consistent with existing Euphoria syntax:
- a type is subroutine
but, no syntax clue to show that this is similar to a regular type
i.e. is rgb() better than rgb in this example?
- can't declare a constant in a subroutine
but this happens in this example
- declarations in a subroutine are local
but these enums are global
This makes documenting enum type difficult (can no longer say Euphoria is coherent.)
Existing enum type
Type function returns the "index number" of each enum, sort of. If you have identical enum values then aliasing happens. Aliasing is not a Euphoric concept.
include std/console.e enum type rgb red, green=3, blue, alpha=1 end type display( "=== enum values" ) ? red ? green ? blue ? alpha display( "=== rgb variable declaration" ) display( " green" ) rgb screen screen = green ? screen ? rgb(screen) display( " blue" ) screen = blue ? screen ? rgb(screen) display( " alpha" ) ? alpha screen = alpha ? screen ? rgb(screen)
Output is:
=== enum values 1 3 4 1 === rgb variable declaration green 3 2 blue 4 3 alpha 1 1 1
Why is there aliasing?
4.3 vs 4.1AL
We now have two different flavors of enum type
enum type color red=4, green=7, black=1, blue=3, pink=10 end type puts(1, "-- enum values\n" ) ? red ? green ? black ? blue ? pink display( "-- enum type values\n" ) ? color(red) ? color(green) ? color(black) ? color(blue) ? color(pink)
4.3
-- enum values 4 7 1 3 10 -- enum type values 1 2 3 4 5
4.1AL
-- enum values 4 7 1 3 10 -- enum type values 1 1 1 1 1
_tom
2. Re: explain enum type to me
- Posted by ghaberek (admin) Oct 15, 2014
- 1327 views
I think the documentation does a pretty good job of explaining it.
There is also a special form of enum, an enum type. This is a simple way to write a user-defined type based on the set of values in a specific enum group. The type created this way can be used anywhere a normal user-defined type can be use.
For example,
enum type RGBA RED, GREEN, BLUE, ALPHA end type -- Only allow values of RED, GREEN, BLUE, or ALPHA as parameters function xyz( RGBA x, RGBA y) return end function
However there is one significant difference when it comes to enum types. For normal types, when calling the type function, it returns either 0 or 1. The enum type function returns 0 if the argument is not a member of the enum set, and it returns a positive integer when the argument is a member. The value returned is the ordinal number of the member in the enum's definition, regardless of what the member's value is. As an exception to this, if two enums share the same value, then they will share the same ordinal number. The ordinal numbers of enums surrounding these will continue to increment as if every enum had a unique ordinal number, causing some numbers to be skipped.
(emphasis mine)
So there you have it. This explains the behavior you're seeing in black-and-white.
include std/console.e enum type rgb red, green=3, blue, alpha=1 end type
Why is there aliasing?
Because red and alpha are both equal to 1. The internal values for the enum type are {1, 3, 4, 1} and so it never gets to that second 1.
Euphoria doesn't really know that red and alpha are named differently. As long as their value is within the list of "good" values, it passes the test.
Here is a basic example of what enum type does with a plain enum and a plain type.
enum red, green=3, blue, alpha=1 constant rgb_values = {red, green, blue, alpha} type rgb( object x ) return find( x, rgb_values ) end type puts( 1, "rgb_values = " ) ? rgb_values -- {1,3,4,1} ? rgb( red ) -- 1 ? rgb( green ) -- 2 ? rgb( blue ) -- 3 ? rgb( alpha ) -- 1 (because alpha=red)
Hope that helps,
-Greg
3. Re: explain enum type to me
- Posted by _tom (admin) Oct 15, 2014
- 1329 views
Hope that helps,
-Greg
Thanks for the reply. Agreed, the existing documentation correctly describes the current behavior.
Now, why the skip of ordinal numbers?
Why is 4.1AL different?
How do you explain the thinking for how the syntax for enum type was chosen?
_tom
4. Re: explain enum type to me
- Posted by DerekParnell (admin) Oct 15, 2014
- 1340 views
Why is 4.1AL different?
How do you explain the thinking for how the syntax for enum type was chosen?
Because Shawn got it wrong in his AL edition. I actually have a lot of reservations about this AL edition and should have spent more time examining it and commenting.
5. Re: explain enum type to me
- Posted by SDPringle Nov 08, 2014
- 1187 views
Please refere to the documentation for 4.1AL. The fact that type enum values return 1, 2, 3, is only an implementation detail. A type routine just needs to return a non-zero atom for truth. In EUPHORIA any non-zero value is true. In one implementation we use a find_from and in another we use comparisons of each value with and gates between them.