class Name in ['0', ] Item, ... class Name in '0' to Decimal type Name in ['0', ] Item, ... type Name in '0' to Decimal
You can declare an enumerated type on either a Class declaration or by using a Type declaration within a Class preamble. Enumerated types cannot have Generic names and cannot be inherited by another enumeration.
A variable with an enumerated type can be assigned to one item from a list of symbolic names or from a range of unsigned decimal integers. A range of integers always starts at zero. An enumerated name must begin with a letter and only contain alpha-numeric characters, dots, and underscores.
Item := Letter { Letter | Digit | '.' | '_' }*
In the following examples, Paint has the enumerated type, Color. It can be assigned a value from the list Red, Blue, and Yellow. All enumerations also include 0; which can optionally lead the list in its declaration.
Example of Enumerated Types class Color in Red, White, Blue Zero is also an implied state. local Paint Color Local variable with an enumerated type
Enumerated values may be compared for equality or inequality. With an inequality the comparison depends on the position in the list in its declaration. You can also compare enumerations using a Comparison List.
Paint = .Yellow Assign an enumeration to a constant. IF Paint = 0 Test if an enuimeration is zero. IF Paint > .Red Test if White or Blue. IF Paint ~= 0 | .Blue Test if neither zero nor Blue. I = Paint Get the position of the current value.
Enumerations can also be used as indecies for arrays. You can declare an array with dimensions whose bounds are an enumerated type. Use the name of the type in place of the dimension bounds.
local A[ Color ] string Local array of stings indexed by an enumerator A[ .Yellow ] = "yellow" Set an
A pointer with an enumerated type cannot reference field within a structure. It can only reference a scalar variable or an element in an array of enumerations.
local V Color, &Scalar enumeration A[9] Color, &Array of enumerations @P Color :Reference an enumeration P = @V; Reference an enumeration variable. P = @A[5]; Reference an element in an array of enumerations.
The form function gets the symbolic name of an enumerated argument. It returns a string with its uppercase name.
Item = .Red; Set enumerated value. Name = form( Item ); Name will be "RED".
Enumerations with up to 255 items are encoded in a Byte. Larger enumerations are allowed with up to 65535 items and are encoded in a Parcel. When casting a symbolic enumeration to an integer type, its value is the position of the symbol in the list. Position 0 is the value 0, position 1 is the first item, position 2 is the next item, and so on.
You can cast an unsigned integer value to an enumerated value by using the name of the enumerated type as the name of the cast function. The argument can be a constant expression or a self-typing integer expression. The operand may also be a star (*) to return the position of the last enumerated value declared.
B = byte{ Paint }; Set the Byte, B to Paint's position. Paint = Color{ 2 }; Set Paint to Blue. Paint = Color{ B + 1}; Set Paint to the position in B + 1. Paint = Color{ * }; Set Paint to Yellow.
For applications an enumerated range defines an upper bound on an unsigned integer. When storing an integer into a range a Fault raised if the integer exceeds the bound. This safety check is useful to ensure computations are within range.
I = Color{ Paint }; Use an implied cast to an unsigned Word. I = Color{ .Red }; Set an integer I to 1. I = Color{ * }; Set an integer I to 3.
Enumerated type casts may also be used in array and do loop ranges. Within an array declaration, if an enumerated cast is used on a bound, the bound can be referenced with either a Word expression or enumerated value without an explicit cast.
local Array[ .Red to Color{*} ] string local Array[ Color{ .Blue } ] double V = Array[ I ]; Index into the array by position. V = Array[ 2 ]; The Index position for Yellow is 2. V = Array[ Paint ]; Index by the position of Paint's value. V = Array[ .Red ]; Index by an enumerated constant. DO I = Color{ .Red } to .Blue: DO over Red to Blue DO I = 0 to Color{ .Blue }: DO over 0 and all Colors DO Paint = .Red to *: DO from Red to the last Color DO Paint = *: DO over all Colors
The Enumerated.Type class in the Gilda.Basis library also has methods that convert strings into their corresponding enumeration values. It also has additional generic procedures for processing enumerations.
<<< In an application Class file my.stuff.clg >>> import Enumerated.Type( Color ): Import in your class file <<< In the application file my.stuff.g >>> ENUMERATE.EXACT Color, "RED"; Set Color to .Red