Skip to content

Records & Row Polymorphism

SPL uses structural typing with row polymorphism, combining JavaScript’s lightweight object ergonomics with full static type checking and IDE completion.

Named struct types are closed by default and compile directly to plain JS objects:

'' Customer profile.
type Person = struct {
firstName: Str '' Given name
age: Int '' Age in years
}
// Anonymous record — structurally compatible with `Person`!
let ada = { firstName: "Ada", age: 36 }
fn is_adult(p: Person) -> Bool {
p.age >= 18
}
print(is_adult(ada)) // true

Extend or update records immutably with { ...base, key: val }:

let user = { firstName: "Ada", age: 36 }
let updated = { ...user, age: 37, role: "engineer" }
// inferred: { firstName: Str, age: Int, role: Str }

Write generic functions that require a subset of fields without losing the caller’s extra fields:

// Accepts any record containing `firstName: Str`
fn greet(x: { firstName: Str, .. }) -> Str {
"Hi " + x.firstName
}
// Inferred: fn({ ..r }) -> { ts: Int, ..r }
fn with_timestamp(x) {
{ ...x, ts: 1700000000 }
}
let tagged = with_timestamp({ firstName: "Ada", role: "admin" })
// `tagged.firstName`, `tagged.role`, and `tagged.ts` are all statically typed!

Hybrid Open Records (..extra: Dict<Str, V>)

Section titled “Hybrid Open Records (..extra: Dict<Str, V>)”

Model JSON payloads, HTTP headers, or tool events that mix statically known fields with arbitrary runtime keys:

type ToolEvent = struct {
id: Str '' Unique event identifier
kind: Str '' Event category
..extra: Dict<Str, Json> '' Arbitrary extra runtime metadata
}
  • Dot access (ev.id): checked statically against known fields with full IDE completion.
  • Bracket indexing (ev["trace_id"]): accesses dynamic keys and returns Option<V> (V?) so missing keys are handled safely.
  • JSON Schema: closed structs emit "additionalProperties": false, while hybrid open structs emit "additionalProperties": <schema of V>.