Documentation contents
Language guide

Values and data

Model the readings from a small network of weather stations with records, tuples, unions and pattern matching.

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.

Numbers, text and booleans

The basic types are bool, text, the sized integers i8 to i64 and u8 to u64, the floating-point types f32 and f64, and bigint.

Write number literals plainly. A whole number such as 3 is an i64 and a decimal such as 34.5 is an f64. Where the surrounding code expects another type, such as a record field or a function parameter, the literal takes that type instead.

State types at the boundaries: on function parameters and results, record fields and inputs. Inside those, let the compiler infer. A suffix such as 255u8 is for the rare literal whose type nothing else decides. The types reference lists every numeric type and the rules for conversions that could lose precision.

Quantities carry their units

Rainfall and wind speed are measurements, so their types say what they measure. The module first declares its dimensions, the quantities built from them and the units that measure those. Units of measure explains the three levels.

dimension Length
dimension Time

quantity Rainfall = Length
quantity Speed = Length / Time

unit m: Length
unit s: Time
unit mm: Rainfall
unit mps: Speed = m / s

Records

A record groups named fields. Declare the type, then build a value by naming the type and giving each field.

type Reading = { station: text; rainfall: f64<mm>; windSpeed: f64<mps> } derive(Eq, Debug, Clone)

node Harbor = Reading { station = "Harbor"; rainfall = 34.5<mm>; windSpeed = 12.0<mps> }
node Ridge = Reading { station = "Ridge"; rainfall = 8.0<mm>; windSpeed = 27.5<mps> }

Read a field with a dot.

fn wetter(a: Reading, b: Reading) -> Reading =
    if a.rainfall >= b.rainfall then a else b

node Wettest = wetter(Harbor, Ridge)
node WettestStation = Wettest.station

Records are values and do not change after they are built. To get a record with some fields changed, write with and the fields to replace, each with a leading dot. Every other field is carried over.

fn relabel(r: Reading, name: text) -> Reading =
    r with { .station = name }

fn sheltered(r: Reading) -> Reading =
    r with { .station = r.station ++ " (sheltered)", .windSpeed = 0.5<> * r.windSpeed }

node Renamed = relabel(Ridge, "North Ridge")
node RenamedStation = Renamed.station
node RidgeStation = Ridge.station

The result is a new record. RenamedStation is "North Ridge" and RidgeStation is still "Ridge".

The derive(Eq, Debug, Clone) clause asks the compiler to supply those traits for the type. With Eq, two readings can be compared with ==.

node SameReading = Harbor == Ridge

Tuples

A tuple groups values by position. A function can return one, and a let can take it apart again.

fn extremes(a: Reading, b: Reading) -> (f64<mm>, f64<mm>) =
    if a.rainfall <= b.rainfall then (a.rainfall, b.rainfall) else (b.rainfall, a.rainfall)

fn spread(a: Reading, b: Reading) -> f64<mm> =
    let (low, high) = extremes(a, b) in
    high - low

Unions

A union is a value that is exactly one of several named cases. A case can stand alone or carry a payload. Give a case that needs several values a record or a tuple as its payload.

type Thresholds = { rainfall: f64<mm>; windSpeed: f64<mps> }

type Alert =
    | Clear
    | Watch(text)
    | Warning(Thresholds)

Build a union value with #, the type name and the case.

fn classify(r: Reading, limits: Thresholds) -> Alert =
    if r.windSpeed >= limits.windSpeed then #Alert.Warning(limits)
    else if r.rainfall >= limits.rainfall then #Alert.Watch("heavy rain")
    else #Alert.Clear

node Limits = Thresholds { rainfall = 30.0<mm>; windSpeed = 25.0<mps> }
node HarborAlert = classify(Harbor, Limits)

Pattern matching

match chooses a branch by case. A pattern names the case and binds its payload to a name, or discards it with _.

fn headline(alert: Alert) -> text =
    match alert {
        Clear -> "all clear"
        Watch(reason) -> "watch: " ++ reason
        Warning(_) -> "warning"
    }

The second form of match puts each arm after a bar. An arm can add a condition with when, and _ on its own matches whatever is left. The result type is i64, so the arms are written as plain whole numbers.

fn severity(alert: Alert) -> i64 =
    match alert with
    | Warning(limits) when limits.windSpeed >= 30.0<mps> -> 3
    | Warning(_) -> 2
    | Watch(_) -> 1
    | _ -> 0

Two rules keep patterns predictable. A pattern binds only the names written in it. Patterns match cases, not literal values, so use if or a when condition to branch on a particular number or piece of text.

Text is joined with ++, as in the Watch arm above.

Run the example

The complete module is station-values.myc. Its test block states the results these definitions produce, and the test manifest runs it. The next page covers records in depth.