Record

The Record module provides functions for dynamic record introspection and manipulation. These are useful when working with records whose structure is not known at compile time.

Destructuring

Pattern Matching in Bindings

The most common way to extract values from records is through destructuring, which lets you bind multiple fields to variables in a single statement.

Shorthand Destructuring
{field1, field2} = record
Extract fields into variables with the same name. The shorthand {name} means {name: name}.
person = {name: "Alice", age: 30, city: "NYC"}
{name, age} = person

println name    # => Alice
println age     # => 30
Rename Destructuring
{field: newName} = record
Extract fields and bind them to different variable names using the {field: newName} syntax.
person = {name: "Alice", age: 30}
{name: user-name, age: user-age} = person

println user-name    # => Alice
println user-age     # => 30
Partial Destructuring
{field1} = record
Extract only the fields you need. Extra fields in the record are ignored.
config = {host: "localhost", port: 8080, debug: true}
{port} = config

println port    # => 8080
Nested Destructuring
{outer: {inner}} = record
Extract fields from nested records in a single pattern.
user = {info: {name: "Bob", email: "bob@example.com"}}
{info: {name, email}} = user

println name     # => Bob
println email    # => bob@example.com

Accessing Fields

Record.get
Record -> String -> Option a
Gets a field value from a record by name. Returns Some value if the field exists, None otherwise.
person = {name: "Alice", age: 30}

match (Record.get person "name")
  | Some name -> println "Name: ${name}"
  | None -> println "No name field"
# => Name: Alice
Record.has-field?
Record -> String -> Bool
Returns true if the record has a field with the given name.
person = {name: "Alice", age: 30}

println (Record.has-field? person "name")  # => true
println (Record.has-field? person "email") # => false

Introspection

Record.fields
Record -> List String
Returns a list of all field names in the record.
person = {name: "Alice", age: 30, city: "NYC"}
fields = Record.fields person
println fields
# => ["name", "age", "city"]

Conversion

Record.to-map
Record -> Map String a
Converts a record to a Map with string keys. This is useful for dynamic field access or serialization.
person = {name: "Alice", age: 30}
m = Record.to-map person

println (Map.get m "name")
# => "Alice"