Skip to main content

Enums

Enums are used to represent types that have multiple variants (sometimes called tagged unions or sum types). These are useful for datums and redeemers.

Example of an enum:

enum Redeemer {
Cancel
Buy { buyer: PubKeyHash }
}

// instantiating an enum:
const my_redeemer = Redeemer::Buy { PubKeyHash::new(#...) }
// type of 'my_redeemer' is inferred

Note: the OOP analogy of an enum is an abstract class, and the enum variants can be thought of as concrete implementations (i.e. child classes).

Note: enum variants without fields don't use braces.

Methods

Like structs, methods can be places inside enum blocks.

The un-annotated self argument of enum methods, is implicitly typed as the enum base type.

Data

Data is a special builtin enum with 5 members:

A switch expression over Data can use any of these as case types:

data.switch{
i: IntData => ...,
b: ByteArrayData => ...,
l: ListData => ...,
m: MapData => ...,
c: ConstrData => ...
}

Note: the default _ case can also be used as a substitute for any of these cases.