Helios language/ User-defined types/


Switch cases and assignments have special syntax for destructuring user-defined structs and enum members.

Destructuring in Helios has the following properties:

  • can be arbitrarily nested
  • is positional (names of the fields don't matter)

Note: destructuring is not pattern matching.

Destructuring assignment

testing destructure_pair

struct Pair {
    first:  Int
    second: Int

func main() -> Bool {
    p = Pair{1, 2};
    Pair{a, _} = p;
    a == 1

You can also assign a name to any intermediate value when destructuring:

pair: Pair{a, _} = p; ...

Optionally you can include the type when destructuring a field:

pair: Pair{a: Int, _} = p; ...

Destructuring an enum instance into an enum variant will create a runtime type assertion:

Option[Int]::Some{a} = option; ... // throws runtime error if option is None

Destructuring an enum member in a switch case

The same syntax as above can be used in switch cases. Destructuring some builtin enums is also possible (WiP).

    None => ...,
    Some{item} => doSomething(item)

Nested destructuring

Nested destructuring requires the type of the nested struct to be specified.

spending my_validator

struct Rational {
    top: Int
    bottom: Int

enum Redeemer {
    Sell{price: Rational}
    Buy{price: Rational}


func main(_, r: Redeemer, _) -> Bool {
        Sell{Rational{top, bottom}} => doSomething(top, bottom),
        Buy{Rational{top, _}} => doSomething2(top)

Nested destructuring is also possible in assignments.