Enumerations

An enumeration is a list of named constants that are encapsulated into a special type of class. Gosu supports enumerations natively, as well as providing compatibility for using enumerations defined in Java. The sections in this topic refer to example enum classes, TaskStatus and FruitType. The source code for these classes is provided at the end of the topic.

Enum types

The Gosu enumeration type (enum) is a reference type and is used to declare distinct enumeration values. An enumeration type inherits from java.lang.Enum. An enum type declaration is a specialized form of class declaration beginning with a list of enumeration value declarations. The declaration of fields, method, nested types for enums are the same as the declarations for ordinary classes. Enumerations must have a comma-delimited list of enumeration constants, followed by the rest of the declarations, including fields and constructors and methods.

Note: The modifiers abstract and final cannot be used.

A nested enum type is implicitly static. The static modifier is allowed but is not required. A nested enum type cannot refer to instance fields of an enclosing type.

Enumeration values

The type of a declared enumeration value is the enclosing enum type. An enumeration value is similar to a public static final field. The ordinal value of an enumeration value is set by its position in the list of values, with the first value being zero. There are no predefined conversions between enumeration values and integers. A value of an enum type always equals a declared enumeration value. Numeric operations such as plus (+) are not supported for enumeration values. You can use comparison operations such as equal (==) or greater than (>) on enumeration values. Comparison operations are useful if there is a logical progression from one enumeration value in the order of definition to the next.

Outside an enum declaration, an enumeration value can be written in its qualified form, such as TaskStatus.Assigned. Alternatively, if the enum type can be inferred, you can use the unqualified form, such as Assigned, as shown in the following example:
uses doc.example.TaskStatus#*

...

if (firstTask.Status < Assigned) {
  myFirstTask = firstTask
  prevStatus = firstTask.Status
  firstTask.Status = Assigned
}

Enumeration constructors

Any constructors for an enum type are private. An enum variable instance cannot be explicitly created by using the new keyword. Any constructors must follow the list of enumeration value declarations.

Enumeration properties

All enum instances have the following properties:
Code
The name of the enum instance. For example: Banana
Name
The name of the enum instance. For example: Banana
Ordinal
The zero-based position of the enumeration value in the enum class definition. For example, the value of the Ordinal property for FruitType.Banana is 1.
Value
The name of the enum instance. For example: Banana
DeclaringClass
The name of the class of the enum instance prefixed by class. For example: class doc.example.FruitType

You can create additional properties on enum classes, as shown in the FruitType enumeration example. The FruitType enum class defines HasPeel, PeelEdible, BestEatingFormat, and LongName properties.

You can access the properties on an enumeration in your code in the same way as properties on an instance any other Gosu class. For example:

print(Banana.HasPeel)
This line prints:
  • true

Enumeration functions

Enums support functions. The following example calls the displayFruitTypeStuff method on a FruitType enum instance.

var myFruitType = FruitType.Banana

myFruitType.displayFruitTypeStuff()
The following lines are printed:
Name: Banana
Code: Banana
Ordinal: 1
Has peel? true
Is peel edible? false
Eat raw or cooked? Raw
Long name: Banana: Peel before eating

Example ordered enumeration class TaskStatus

This enum class has enumeration values that have a meaningful order.

package doc.example

enum TaskStatus {
  Blocked,
  WaitingForCustomer,
  New,
  Assigned,
  InProgress,
  Complete
}

Example enumeration class with complex structure FruitType

This enum class demonstrates multiple constructors, properties, methods, and a nested enum definition.

package doc.example

enum FruitType {
  Apple(true, true, EatingFormat. Both),
  Banana(),
  Orange(),
  Strawberry(false, null, EatingFormat.Raw)

  // Properties
  var _hasPeel : Boolean as readonly HasPeel
  var _isPeelEdible : Boolean as readonly PeelEdible
  var _bestEatingFormat : EatingFormat as readonly BestEatingFormat

  // No-argument constructor sets default propery values
  private construct() {
    setProperties(true, false, EatingFormat.Raw)
  }

  // Constructor with arguments sets explicit property values
  private construct(hasPeel : Boolean, isPeelEdible : Boolean, eatingFormat : EatingFormat) {
    setProperties(hasPeel, isPeelEdible, eatingFormat)
  }

  private function setProperties(hasPeel : Boolean, isPeelEdible : Boolean, eatingFormat : EatingFormat) {
    _hasPeel = hasPeel
    _isPeelEdible = isPeelEdible
    _bestEatingFormat = eatingFormat
    _longName = _hasPeel and not _isPeelEdible ? "${Name}: Peel before eating": Name
  }

  // Utility method
  function displayFruitTypeStuff() {
    print("Name: ${Name}")
    print("Code: ${Code}")
    print("Ordinal: ${Ordinal}")
    print("Has peel? ${HasPeel}")
    print("Is peel edible? ${PeelEdible}")
    print("Eat raw or cooked? ${BestEatingFormat}")
    print("Long name: ${LongName}")
  }

  // Nested enum
  enum EatingFormat {
    Raw, Cooked, Both
  }
}