Enumerations

An enumeration defines a common type for a group of related values and enables you to work with those values in a type-safe way within your code.

Enumeration Syntax

You introduce enumerations with the enum keyword and place their entire definition within a pair of braces:

public enum Option<A> {
case Some(A)
case None
}

Matching Enumeration Values with a Match Expression

You can match individual enumeration values with a match expression:

let a = Some(1);
match a {
case Some(i) => {
print("Some")
}
case None => {
print("None")
}
}

Method

Methods are functions that belong to instances of a particular enumeration. You write an instance method within the opening and closing braces of the enumeration it belongs to.

Here’s an example that defines a simple Option class, which is defined in the standard library:

public enum Option<A> {
case Some(A)
case None
public isSome(): boolean {
match this {
case Some(_) => true
case _ => false
}
}
public isNone(): boolean {
match this {
case None => true
case Some(_) => false
}
}
}

The Option class defines two instance methods:

  • isSome() judges if the enum is in the case of Some
  • isNone() judges if the enum is in the case of None

You call instance methods with the same dot syntax as properties:

let a = Some(1);
print(a.isSome()); // => true
print(a.isNone()); // => false