Records and Types

A record type describes the fields a value supplies. GCSL uses structural types: two declarations with the same structure describe the same type, and a record can be used where fewer of its fields are required.

Declaring a shape #

Use type to give a structure a reusable name. An annotation checks a value against that structure. Defaults supply omitted fields in a record literal checked against the type:

type Install = {from: String, exclude: List<String> = []}
let item: Install = {from = "src"}
item.exclude

Defaults are constant expressions, not arbitrary function calls. They are applied where a record literal has the expected type; they don't add fields to an already constructed variable with a smaller type.

An annotation also determines what fields later code can see. Extra fields in a record literal produce a warning when an expected type hides them. A structural alias does not introduce a distinct runtime identity.

Generic types #

A type can take type parameters, named in angle brackets after its name. Each use names the types to put in their place:

type Pair<A, B> = {first: A, second: B}
let entry: Pair<String, Int> = {first = "port"; second = 8080}
entry.second

A use must give as many type arguments as the type has parameters, and a type without parameters takes none. Because types are structural, Pair<String, Int> is the same type as {first: String, second: Int}. A generic type can be built from another, as in type Named<T> = Pair<String, T>. It can't refer to itself, and it can't spread one of its parameters, because the parameter might not be a record.

Enums #

An extensible enum is a type whose variants any module can add. Declare the enum, with an optional bound that every variant's payload must fit, and then its variants, each with a payload type:

enum Shape extensible<{*: Int}>
enum Shape::Square<{side: Int}>
enum Shape::Point
square = Shape::Square({side = 3})

A variant is also a value: Shape::Square on its own has the type Variant<Shape, {side: Int}>. Calling it makes a value of the enum, and you can pass it to a function as a key. kind.matches(value) tests whether a value is of that variant, and kind.payload(value) takes out its payload:

enum Tool extensible
enum Tool::C<{cc: String}>
fn find<T>(kind: Variant<Tool, T>, tools: List<Tool>, fallback: T): T {
    for t in tools { if kind.matches(t) { return kind.payload(t) } }
    fallback
}
cc = find(Tool::C, [Tool::C({cc = "gcc"})], {cc = "cc"}).cc

Unlike records, enums are nominal: two enums are different types even if they look alike. value is Tool tests whether a value is of the enum. Values of an enum are compared with == only when the enum has a bound, because an unbounded payload might hold a function. A variant is visible in its own file, to the other files of its directory and the directories below it, and to every file that imports its module.

Partial records #

Partial<T> holds any of the fields of the record type T. Use it to describe a record by only some of its fields, and .matches() to test a whole record against it:

type Platform = {arch: String, os: String}
let wanted: Partial<Platform> = {arch = "x86_64"}
fits = wanted.matches({arch = "x86_64"; os = "linux"})

A field of a partial record can't be read by name, because it might be absent.

Records of any fields #

A rest-field, *: T, gives the type of every field that a record type doesn't name. Use it when the field names are data, such as paths. A {*: String} holds any fields, each a String. Named fields keep their own types, so {to: String, *: Int} holds a String field to, and any other fields are Ints:

let files: {*: String} = {"bin/*" = "bin/*"; "sbin/*" = "sbin/*"}
files

Write the record literal where the type is expected: in an annotation, an argument or a definition's result. There, each field that the type doesn't name is checked against the rest-field. A value whose own type has no rest-field doesn't fit one, because a record keeps fields that its static type hides, and they could be of any type. *: * is the exception, as Open records describes.

Read a named field as usual. A field that only the rest-field covers can't be read by name, and a record whose type has a rest-field can't be spread. Typed equality compares every field, and is and as test each field that the rest-field covers.

When the names themselves are only known as the program runs, such as a program's environment built from a list, make the record with record. It takes a list of entries, each an Entry<T>, a {key: String, value: T}, and gives a {*: T} with a field for each:

let vars = [{key = "LANG"; value = "C"}, {key = "TZ"; value = "UTC"}]
let env: {*: String} = record(vars)
env == ({LANG = "C"; TZ = "UTC"}: {*: String})

Each field is named as the program names it, so the record equals one written with the same fields, in any order. A key given twice is an error when the program runs.

entries does the reverse. It lists a record's fields as entries, sorted by key, so two equal records always give the same list:

let deps: {*: String} = {"org.gnu.glibc" = ">= 2.44"; "org.gnu.bash" = ""}
entries(deps)

The record's type must have a rest-field, because a record can hold fields that its type hides. Without one, the type doesn't say what those fields are, so read the fields it names by name instead. An entry's value has a type that covers every field, named or not: the entries of a {to: String, *: Int} have values of type Int | String. record(entries(r)) equals r.

Open records #

A rest-field whose type is * holds any data. An open record type names the fields it knows about and accepts others, so the LSP still completes the known fields:

type Service = {name: String, *: *}
let web: Service = {name = "web"; limits = {cpu = 2}}
(web as {limits: {cpu: Int}}).limits.cpu

Data is a scalar, a File, a Dir, or a record or list of data, or a value of an enum whose bound is data. A function, a secret or a Sandbox doesn't fit *, and neither does a type parameter. A value that might still be a future is awaited where it is given, at any depth, so that is and as find it present. * is written only as a rest-field's type: {a: *} and List<*> are errors.

Any record type fits *: * if its named fields are data, with or without a rest-field of its own, so {version: String, *: *} accepts a value of type {version: String}.

A field that * covers can't be read by name, because nothing says what type it has. Say what you expect with as, which fails when the program runs, naming the field, if the value isn't that type. is asks the same question as a Bool:

fn dependencies(pkg: Package) -> {*: String} {
    if pkg is {ppk: {dependencies: {}}} {
        = (pkg as {ppk: {dependencies: {*: String}}}).ppk.dependencies
    } else {
        = {}
    }
}

The is here tests only that the field is a record, so that a field of the wrong shape fails at the as, and isn't taken for a missing one. Every field of the type tested must be present: a field's default doesn't apply to a test. An is that narrows a variable narrows it to the type tested, so read the fields you know from outside the branch. In the else branch, the variable keeps its type.

entries lists an open record too. Each entry is a {key: String, *: *}, so read its value with as or is, as you would any field that * covers. This lists every field the record holds, including any field its type hid. A test of a hidden field that isn't data, such as a function, is false, and as fails.

A record type with no rest-field already fits {*: *}, so as {*: *} tests nothing and leaves the type as it was. To list a field you only know is a record, such as ppk after if pkg is {ppk: {}}, give it the type instead: let meta: {*: *} = pkg.ppk, then entries(meta).

json.decode can't decode into *, because a document doesn't say what type each value should be.

Combining fields #

Dotted definitions construct nested records. Compatible dotted paths merge, including across files in a workspace:

server.host = "localhost"
server.port = 8080
server.tls.enabled = true

A whole-field definition such as server = {...} closes that field to further dotted extensions. Define the individual dotted leaves when you want several files to contribute to a shared structure.

A spread starts from another record's visible fields. Inside an ordinary record block, put defaults first and explicit overrides afterward:

let defaults = {host = "localhost"; port = 80}
let config = {
    ...defaults
    port = 8080
}
config.port

A spread copies only fields visible through its operand's static type. It cannot replace an earlier explicit field inside an ordinary block. Top-level declarations are resolved across the whole input and take precedence over a spread of the same name regardless of textual order.

Unions and narrowing #

A union, such as Int | String, allows more than one shape. Use is to test a value and as for a checked conversion that fails at runtime if the value doesn't fit. In if name is Type, the branches see narrowed copies of the named value. This does not perform general flow analysis of later statements or dotted field paths.

Typed equality == compares operands of the same static type. Total equality === compares complete runtime records, including fields hidden by their shared static type. Both require the same static operand type. The language's restrictions on unions, functions, and futures are described in gcsl(5) and Inference Limits.

Edit this page