Documentation contents
Language guide

Matching on cases

Take unions apart with match: arms in order, conditions on arms, two values at once, and what happens when no arm fits.

On this page

Myceta 2.0. These pages describe the language as planned for 2.0, the first public release, which is still in development. Details may change before release. The examples were type-checked and tested with the development compiler.

Naming a payload or discarding it

Values and data introduced match. A rain reading can come from a gauge, from radar, or from an estimate, and only a gauge has a name.

type Source =
    | Gauge(text)
    | Radar
    | Estimate

fn sourceName(source: Source) -> text =
    match source {
        Gauge(name) -> "gauge at " ++ name
        Radar -> "radar"
        Estimate -> "estimate"
    }

When an arm does not need the payload, write _ in its place. A _ on its own, as a whole pattern, matches every case not already handled.

fn trusted(source: Source) -> bool =
    match source with
    | Gauge(_) -> true
    | Radar -> true
    | _ -> false

Arms are tried in order

The first arm whose pattern fits, and whose when condition holds, gives the result. Put the narrowest condition first and let later arms catch what is left.

type Sky =
    | Dry
    | Wet(f64<mm>)

fn rainBand(sky: Sky) -> text =
    match sky with
    | Wet(rain) when rain >= 30.0<mm> -> "heavy"
    | Wet(rain) when rain >= 10.0<mm> -> "moderate"
    | Wet(_) -> "light"
    | Dry -> "dry"

Here 34.5 mm is heavy and 2 mm is light. Had the 10 mm arm come first, the heavy arm could never be reached.

Matching two values together

To decide on two values at once, match on a tuple of them. Each arm then gives a pattern for both.

type Wind =
    | Still
    | Gusting(f64<mps>)

fn outlook(sky: Sky, wind: Wind) -> text =
    match (sky, wind) {
        (Dry, Still) -> "calm"
        (Wet(rain), Gusting(speed)) when rain >= 30.0<mm> && speed >= 20.0<mps> -> "storm"
        (Wet(_), _) -> "rain"
        (Dry, Gusting(_)) -> "windy"
    }

Heavy rain with strong gusts is a storm. Heavy rain with light gusts falls through to the third arm and is reported as rain.

When no arm fits

The compiler warns when a match leaves a case of the union unhandled.

fn band(sky: Sky) -> text =
    match sky {
        Wet(_) -> "wet"
    }
// warning: the match on Sky is missing the case Dry

The module still compiles. A node that reaches the match with a Dry value fails, and its outcome says that no arm accepted the value. Nodes that depend on it do not run, as the outcomes reference describes.

Prefer listing every case to ending with _. When someone later adds a case to the union, each match that lists cases is flagged, while a match that ends with _ quietly sends the new case to its catch-all arm.

Branching on text and numbers

Patterns match cases, not literal values. To choose by a particular piece of text or a number, use if, or a when condition on an arm.

fn station(code: text) -> text =
    if code == "H" then "Harbor"
    else if code == "R" then "Ridge"
    else "unknown"

If the same codes come up in many places, consider a union with a case for each, so that the compiler can check every match on them.

Run the example

The complete module is station-match.myc, run by the test manifest. The next page covers functions and control flow.