Files
Decodal/doc/manual/source/language/expression/object.md
T

2.2 KiB

Object Expression

An object expression is a collection of named fields.

{
    host = "127.0.0.1";
    port = 8000;
}

Objects can represent both configuration values and schemas.

MyConfig = {
    host = String;
    port = Int default 8080;
};

Dot-path fields

A nested field can be defined with a dot path.

{
    feature_hoge.enable = false;
}

This represents the same structure as:

{
    feature_hoge = {
        enable = false;
    };
}

Map constraint

An associative-array constraint applies the same schema to every field value in an object.

Services = {...{
    port = Int;
    enabled = Bool default true;
}};

{...T} represents an object whose keys are not fixed and whose values all satisfy T. An empty object is allowed. After materialization, it becomes the same object data as an object with named fields.

services = {
    api = { port = 8080; };
    worker = { port = 8081; enabled = false; };
} as Services;

Each entry is refined by as into the right-hand field domain, so a field other than port or enabled on the left produces an error. A field found only on the right remains abstract whether or not it has a default. An object supplied by the host can preserve string keys that are not valid identifiers, although field names written in DCDL source are limited to ordinary identifiers.

Object rest constraint

Fixed fields and a value range for arbitrary keys can coexist in one object.

OpenConfig = {
    enabled = Bool default true;
    ...Unknown
};

...T applies only to remaining fields that were not named explicitly. Each named field uses its own range; the rest range is not additionally applied to it. A rest constraint does not generate fields. Materialization outputs only additional fields that actually exist.

{
    enabled = false;
    plugin = { name = "cache"; };
} as OpenConfig

An object can contain one ...T, at its end. An object without it is closed, and an undeclared left-hand field under as produces an error. {...T} with no named fields is an abstract map constraint; materializing it by itself requires a default or a concrete value.