Contracts

Named types, typed fields, and validation at the boundary.

What a contract is

A named type declares its fields, their types, and any defaults. Use the type name to construct a value and in bindings and parameters.

Session::(
    id: string
    principal_id: string
    expires_at: number
    active: bool = true
)

session: Session := Session(id = "s1", principal_id = "u1", expires_at = 1000)
print session/active     // → true (default filled in)

Check data against a named type

Bind incoming data to a named, closed type. The binding rejects unknown fields and missing required fields and checks the types of the values. optional(T) expresses possible absence. list(T) and map(K, V) check the types of collection entries.

User::(name: string, age: int)

// A typed binding rejects an unknown field in decoded data.
try
{
    user: User := eval('[name = "Ada", age = 30, extra = 1]', execute = false, duplicate_keys = "reject")
}
catch { print "unknown field rejected" }

// ...and missing required fields.
try
{
    user: User := eval('[name = "Ada"]', execute = false, duplicate_keys = "reject")
}
catch { print "missing field rejected" }

Why bother

Check data against its contract when it crosses a boundary such as an HTTP request, an on-disk snapshot, or a call to a functor.

Optional fields and defaults

UserPrincipal: static :(
    id: string
    display_name: string
    email?: string          // optional — may be null
    active: bool = true     // default — caller may omit
)

u := UserPrincipal(id = "u1", display_name = "Ada")
print u/active              // → true
print u/email ?? "no-email" // → "no-email"

field?: type marks the field optional (its value may be null); the ? attaches to the name, not the type. field: type = value gives a default. field: type alone declares a required field of that type.

Field-or-default in one line

Declare fields that can be absent. ?? and string(value, default = ...) provide a default for null.

ServerConfig::(name: string, port: int)
AppConfig::(server: optional(ServerConfig) = null)
cfg: AppConfig := AppConfig()

port: int := cfg/server/port ?? 8080     // null absorbs the chain → 8080
label: string := string(cfg/server/name, default = "unnamed")
print "{label}:{port}"                   // → "unnamed:8080"

Closed by default, open by declaration

Reading an undeclared member off a functor instance throws a catchable Unknown name error by default — a typo surfaces at the site, not three calls later as a mysterious null. A functor declared open opts out: missing members read as null, and x/field ?? default becomes the idiomatic probe. Contracts stay data-only on purpose — behavior lives in plain functors that take the record as input, which is what keeps records portable across processes and storage backends.

Wrapper functors with `=`

The one method-shaped exception is =. A typed wrapper functor — a sub-functor that holds a value plus metadata — can override how assignment lands by defining a = method in its parameter block. The runtime fires it on := at construction and on = afterwards, and sibling metadata stays reachable through field access:

ff_string ::(
    value: string = ""
    =::(rhs: string) { ../value = rhs }
)

Greeting :( name: ff_string :(description = "Display name") )

g := Greeting(name := "world")    // construction-time `=` fires
print g/name/value                 // → "world"
print g/name/description           // → "Display name"

g/name = "second"                  // post-construction `=` fires
print g/name/value                 // → "second"
print g/name/description           // → "Display name"  (preserved)

This is the form-builder pattern: the facet package attaches per-field description / placeholder / widget metadata alongside each value, and the form generator walks the contract reading both as siblings. The = hook fires only when the wrapper's type defines one — plain typed slots keep ordinary overwrite semantics.

Common pitfalls