5.1.4 Enumerated Type Declaration


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.


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.


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.


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.


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.


The form function gets the symbolic name of an enumerated argument. It returns a string with its uppercase name.


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.


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.


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.


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.

Structure Type Declaration

Import Class Declaration