SultanC
  1. Home
  2. Examples
  3. Data

Types with variants, and matching on them

data/enums.sn
type Operation = | Add of int | Multiply of int;

def apply(value: int, operation: Operation): int {
    match operation with
    | Operation.Add(amount) -> { return value + amount; }
    | Operation.Multiply(amount) -> { return value * amount; };
}

def main(): int {
    let first: Operation = Operation.Add(1);
    let second: Operation = Operation.Multiply(2);
    return apply(apply(20, first), second);
}

Run it

With the interpreter, without compiling:

./build/sultanc run examples/en/data/enums.sn

Or compile it to an executable:

./build/sultanc examples/en/data/enums.sn -o build/example && ./build/example

Source on GitHub   All examples