Documentation contents
Language guide

Records in depth

Records that take a type, fields computed from other fields, updates with with, records without a declared type, and taking tuples apart.

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.

Records that take a type

A record can take a type parameter, so that one declaration serves several element types. A span of expected rainfall and a span of gauge codes share the same shape.

type Span<T> = { low: T; high: T }

node Expected = Span { low = 5.0<mm>; high = 20.0<mm> }
node Codes = Span { low = "A"; high = "F" }
node FirstCode = Codes.low

The compiler works out T from the fields: Expected is a Span<f64<mm>> and Codes is a Span<text>. A few type names, such as Map and Pair, come with the standard library and cannot be declared again.

Fields computed from other fields

Mark a field derived and give the expression that computes it. The storm's total rainfall follows from its rate and its duration, and the units check: millimetres per hour times hours is millimetres.

quantity RainRate = Rainfall / Time

unit h: Time
unit mmph: RainRate = mm / h

type Storm = {
    station: text;
    rate: f64<mmph>;
    duration: f64<h>;
    derived total: f64<mm> = rate * duration
}

node Squall = Storm { station = "Harbor"; rate = 12.0<mmph>; duration = 1.5<h> }
node SquallTotal = Squall.total

Read a derived field like any other. SquallTotal is 18 mm. A derived field cannot be given a value: a record literal that sets total is refused, and so is a with update of it. The field always agrees with the fields it is computed from.

Updating a record

with makes a new record that differs from an existing one in the fields you name. Derived fields are computed again for the new record.

fn longer(storm: Storm, extra: f64<h>) -> Storm =
    storm with { .duration = storm.duration + extra }

node Lingering = longer(Squall, 1.0<h>)
node LingeringTotal = Lingering.total

The storm that lingers an extra hour has a total of 30 mm, and Squall itself is unchanged. Use with on records whose type takes no parameter. Updating a Span<T> this way is not yet supported, so build the new value with the record syntax.

Records without a declared type

A literal between {| and |} makes a record without declaring a type for it. It suits a value built and read in one place.

node Summary = {| station = "Ridge"; days = 3; wettest = 31.0<mm> |}
node SummaryDays = Summary.days

Once a value is passed between functions or stored in a collection, declare a named type for it, so that every signature can name it.

Taking tuples apart

let takes a tuple apart into names. Inside a block, var does the same and allows the names to be given new values later.

fn ends(span: Span<f64<mm>>) -> f64<mm> =
    let (low, high) = (span.low, span.high) in
    high - low

fn range(readings: (f64<mm>, f64<mm>)) -> f64<mm> = {
    var (first, second) = readings;
    if first > second then first - second else second - first
}

A tuple is data with positions, not a sequence: it has a fixed number of parts, and each part keeps its own type.

A field of a value that may be missing

When the record itself is optional, match on it and read the field in the some arm. The result stays optional, and the caller decides what to do when there is none.

type Gauge = { station: text; serial: text }

fn serialOf(gauge: optional<Gauge>) -> optional<text> =
    match gauge {
        some(g) -> some(g.serial)
        none -> none
    }

node Installed = some(Gauge { station = "Harbor"; serial = "H-7" })
node InstalledSerial = serialOf(Installed)
node InstalledLabel = InstalledSerial ?? "no gauge"

Run the example

The complete module is station-records.myc, run by the test manifest. The next page covers matching on cases.