enum - pannous/warp GitHub Wiki

enums aka variants are aliases which are only needed for interop, otherwise all node_pointer are prone to matching

enum

An enum names its cases. Each case is its own value, of the enum's type:

enum Coin { Penny, Nickel, Dime }
Coin.Dime      // Dime
Dime is Coin   // yes

Coin::Dime and Swift's .dime name the case too.

Cases and numbers

A case is not an int. It becomes one only when you ask, and you say how to count:

Coin.Dime as index      // 2, counting from 0, as C does
Coin.Dime as position   // 3, counting from 1, as # does

An index and a position are the two kinds of indexing counts: xs[Coin.Dime as index] and xs#(Coin.Dime as position) find the same item. For C, a plain as int gives the index.

The other way round, an int is an index (Planned: card enum-from-int):

2 as Coin   // Dime
Coin(2)     // Dime
9 as Coin   // error: 9 is no Coin case

An int is never a case on its own. Calling f(c: Coin) with f(1) is a type error, and its fix offers f(1 as Coin).

Matching

A match on an enum must name every case, or end with other:

cents(c: Coin) := match c { Penny: 1; Nickel: 5; Dime: 10 }

Leaving out Dime is the compile error "match on Coin misses Dime".

Cases with values

When one case carries values, the enum is a tagged union, see variant: enum Shape { Circle(r), Dot }.

On hold: written values, C's enum Coin { Penny = 1, Dime = 10 }. WebAssembly's WIT enums carry no values, so a case is known by its name and its place only; an FFI that needs C's numbers works around it.