The Freya Programming Language

Enumerations

See also

Enumerations are value types that are declared by defining a set of named constants.

Syntax

The simplest form of enumeration looks like this:

Planet = (Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto);

Each identifier declared in an enumerative type is considered a literal constant. Since they are declared inside the scope of the type, you need to qualify these constants at least with the type name, in order to access them from outside the type declaration:

var allowedTargets := new Array[Planet]![Planet.Mars, Planet.Jupiter, Planet.Saturn];
var target: Planet := Planets.Mars;
if EnoughFuel and Planet.Jupiter in allowedTargets then
    target := Planets.Jupiter;

The above defined type is conceptually equivalent to this declaration:

Planet = static class : System.Enum
public
    const Mercury  Planet = 0;
    const Venus: Planet = 1;
    // ...
    const Pluto: Planet = 8;
end;

By default, the value of first constant declared is zero, and each successive constant get the previous assigned value plus one.

Base type for enumerations

A base type can be specified for an enumerative type:

BasicColor = (Red, Green, Blue) : Byte;

If you don't supply an explicit base type, it is assumed to be System.Int32. The only allowed base types are the predefined integer types.

Explicit enumerative initializers

You can also assign an explicit value for enumerative constants:

BasicColor = (Black = 0,
    Red = $FF, Green = $FF00, Blue = $FF0000,
    Yellow = Red | Green, Magenta = Red | Blue, Cyan = Green | Blue,
    White = Red | Green | Blue);

As the above example shows, you can use constants from the same enumerative type in the expression of other constants. Circular constant definitions are not allowed, of course.

See also

The Freya Programming Language
Type declarations
Class declarations
Record declarations
Interface types
case statements
for statements