ava/notes/enumerations.md

58 lines
873 B
Markdown

# Enumerations
Enumerations are sum types.
## Syntax
An enumeration must have at least one type member.
```
enum <name>
-- records or objects...
end enum
```
## Objects
An _object_ is a special type of singleton. Much like the `()` type, each object
is an instance of itself (and the only instance of itself). Objects are used to
represent enumeration cases that do not have any inputs.
## Example: The Option Type
```
given A
enum Option
given A
record Some (value: A)
object None
end enum
given A
fn some: (a: A) => Some[A]
Some (a)
end fn
```
## Example: The Either Type
```
given L, R
enum Either
given L
record Left (value: L)
given R
record Right (value: R)
end enum
given L
fn left: (left: L) => Either[L, Nothing]
Left(left)
end fn
given R
fn right: (right: R) => Either[Nothing, R]
Right(right)
end fn
```