Records & Data
Hale models structured data with nominal type records, tagged enum unions, and type-tagged set unions (A | B | C). Every data type is checked at compile time, copied by value with automatic copy-on-write optimization, and matched exhaustively with match.
type Book { id: Int; title: String; }type Trade { qty: Int; px: Decimal<2>; }type Inbound = Book | Trade;
fn describe(msg: Inbound) -> String { return match msg { Book(b) -> f"book #{b.id}: {b.title}", Trade(t) -> f"trade {t.qty} @ {t.px}", };}
fn main() { let m: Inbound = Trade { qty: 10, px: 42.50d }; println(describe(m));}Data Declarations Overview
Section titled “Data Declarations Overview”| Construct | Syntax Example | Description |
|---|---|---|
Record (type) |
type Player { id: String; score: Int = 0; } |
Fixed-layout product type with optional field defaults |
Enum (enum) |
type Event = enum { Tick(Int), Halt }; |
Tagged union with unit or payload-carrying variants |
Set union (|) |
type Inbound = Book | Trade | Unknown; |
Union of existing struct/enum types where the member type is the tag |
| Type alias | type Cents = Int; / type UserId = String; |
Transparent synonym for an existing type |
| Fixed array / slice | [Int; 8] / [Player] |
Inline fixed-length array ([0; 8]) or slice view |
| Tuple | let pair = (1, "one"); |
Lightweight anonymous grouping |
Records (type)
Section titled “Records (type)”Declare a record at the top level of a file with type Name { ... } and construct it with a struct literal:
type Config { host: String = "127.0.0.1"; port: Int = 8080;}
fn main() { let c = Config { port: 9000 }; // `host` uses its default "127.0.0.1" println(c.host, ":", c.port);}- Compile-time field verification: Unknown field names, missing required fields (fields without defaults), or mismatched field types fail compilation at the literal—including for cross-seed records constructed as
alias::Config { ... }. - A record cannot hold itself by value:
type Node { next: Node; }(orAholding aBthat holds anA) has no smallest value, andhale checkrefuses it at the field that closes the ring. Hold children in abounded[Node; N](it starts empty), or in an enum variant beside one that holds none (type Link = enum { End, Next(Node) };), or keep them elsewhere and hold a key. - A record field’s default names its type: write
meta: Meta = Meta { };, notmeta: Meta = { };. A bare{ }is an empty block, which produces no value, andhale checkrefuses it at the default and spells the literal (Meta { }). - Top-level only:
typedeclarations must appear at the top level outside anylocusbody. - No loci inside records: Because records are pure values with no lifecycle hooks, a
typefield (orenumvariant) cannot hold alocusor a function pointer whose signature names a locus (fn() -> Worker). Store loci inside another locus’sparamsinstead. (A record field typed by aninterfaceis permitted because it is a non-owning reference.)
Optional Fields (x?: T)
Section titled “Optional Fields (x?: T)”A field marked ? may be absent. x?: T declares x and, right after it, a real Bool field has_x that says whether x holds a value; x?: T clearable adds clear_x as well, for a PATCH field whose null means “clear it”:
type Ticker { symbol: String; last?: Decimal<8>; expires_at?: Time clearable; limit?: Int = 100;}
fn main() { let mut t = Ticker { symbol: "BTC" }; // `last` left out: absent println(t.has_last); // false t.last = 97000.5d; // a write sets the flag println(t.has_last); // true println(t.limit, t.has_limit); // 100 false: absent, at its default}- A write sets the flag: a literal that spells
x, an assignment to it through any path (t.last = v,self.row.last = v,rows[i].last = v), a write into a part of it, and apushorsetinto an optionalboundedfield all sethas_x(and clearclear_x). The flags are ordinary fields, so a program may also read and write them by name. - Leaving it out builds it absent:
xthen holds its default, or its type’s zero (0,"",false, the epoch, an empty list, an enum’s first variant, a record of zeros). A type with no zero (a tuple, a point,Bytes) needs a default, andhale checksays so at the field. - One shape on the wire: the flags are exactly the hand-written presence pair (
x: T; has_x: Bool;) and triple the JSON and protobuf codecs read, so a record converted from the pair tox?crosses the wire, describes itself and hashes byte for byte as before (see JSON and The API surface). - Only record fields: Hale has no optional type.
?on a parameter, a local or a locus param, andT?after any type, are refused; a value that may be missing in a computation isfallible(E)(Fallible values).
Value Copy Semantics and Mutation
Section titled “Value Copy Semantics and Mutation”Independent Value Copies and Copy-on-Write
Section titled “Independent Value Copies and Copy-on-Write”Binding a record, String, Bytes, tuple, or array from a field or variable (let saved = self.row; or copy = saved;) gives the binding an independent logical copy of the value:
let saved = self.row;self.row = Row { }; // `saved` retains the previous valuelet mut copy = saved;copy.score = 9; // `saved.score` is unchangedUnder the hood, the compiler avoids copying whenever no mutation can alias the read, and performs a copy on the first write into a local derived from a field, match payload, for element, or tuple.
In-Place Read-Modify-Write Optimization
Section titled “In-Place Read-Modify-Write Optimization”To update a nested array or field inside a locus, either assign directly (self.row.scores[0] = 9;) or read into a local, mutate, and write it back:
let mut scores = self.row.scores;scores[0] = 9; // `self.row.scores` is unchanged until written backself.row.scores = scores; // optimized to an in-place write with zero allocationsWhen a local read from a field is mutated and written back to the same field with no intervening reads of that field, the compiler updates the field’s storage in place without allocating or copying.
Reference Semantics for Parameters, @form(vec) Iteration, and Locus Handles
Section titled “Reference Semantics for Parameters, @form(vec) Iteration, and Locus Handles”| Target | Mutation Behavior |
|---|---|
Record function parameter (fn bump(r: Row)) |
Passed by reference to the caller’s value; r.scores[0] = 9; mutates the caller’s record. |
@form(vec) loop variable (for x in v.items) |
Visits the vec’s cells in place; writing through x updates the stored element. |
Locus field via handle (l.name = s;) |
Copies s into locus l’s arena and recycles the replaced value so repeated external writes never grow l’s arena. |
A method that writes into a record parameter (fn put(x: Bag, p: Bytes) { x.b = p; }) writes the caller’s record, so the value is kept as long as that record, never only for the call, whether the method is fallible(E) or not (and when it then fails). The method may pass the record on to another writer, directly or through a block, if or match, and the same holds at every step. A record a field of self holds (put_free(self.bag, p)) is kept in the locus’s arena. One call can hand a writer only one owner’s records. self.put_two(self.held, x, p), or self.put(if c { self.held } else { x }, p), mixes the locus’s record with the caller’s and is rejected. Copy one side into a local record first (let mine = Bag { b: x.b };) and hand that, or write the held record through self.
Such a write is not recycled when the method returns, and does not replace the old value in place the way self.bag.b = p does. Handing a writer self.bag once per message, or once per pass of a run() loop, keeps every value until the locus dissolves, and hale check reports the call as an unbounded allocation. Write the held record through self instead, or hand the writer a record the loop body builds.
Arrays ([T; N]) and Tuples
Section titled “Arrays ([T; N]) and Tuples”Use [T; N] for fixed-length inline arrays, [T] for slice views, and (a, b) for small ad-hoc tuples:
type Match { players: [Player]; } // slice field
let xs = [1, 2, 3]; // [Int; 3]let zeros = [0; 8]; // [Int; 8] filled with 0let pair = (1, "one"); // tupleFor growable lists, use a @form(vec) locus or bounded[T; N] (Lists & Maps).
Type Aliases (type Name = Target;)
Section titled “Type Aliases (type Name = Target;)”A type alias introduces a transparent synonym for an existing type:
type Cents = Int;
fn price_of(n: Cents) -> Cents { return n * 2; }
fn main() { let c: Cents = 250; println(price_of(c));}Because Cents is transparent, it is completely interchangeable with Int (including in struct construction RowAlias { id: 1 } and enum patterns LightAlias::Red). If you need the compiler to reject mixing two integer types, use distinct Int (Units, End to End) or a single-field struct (type Cents { v: Int; }).
Type Alias Rules
Section titled “Type Alias Rules”- Literal construction requires a record or locus target: If
type Cents = Int;, writingCents { }is a compile error. - No cyclic aliases:
type A = B; type B = A;is rejected. - Concrete generic aliases only: An alias can name one instantiation of a generic record or locus (
type OrderReply = Envelope<Order>;), andlet r: OrderReply = Envelope { data: o };then takes its type arguments from the alias. An alias cannot take parameters itself:type Reply<T> = Envelope<T>;is refused.
Enums (enum) and Exhaustive match
Section titled “Enums (enum) and Exhaustive match”An enum defines a tagged union whose variants may be unit tags or carry positional payloads:
type Event = enum { Tick(Int), Trade(Decimal, Int), Halt,};
fn handle(e: Event) { match e { Event::Tick(0) -> println("tick zero"), Event::Tick(n) -> println("tick #", n), Event::Trade(price, size) -> println("trade ", size, " @ ", price), Event::Halt -> println("halt"), }}
fn main() { handle(Event::Tick(0)); handle(Event::Trade(19.99d, 5));}- Construction and formatting: Construct variants with
EnumName::Variant(Event::Halt,Event::Tick(3)). Printing an enum outputs its declared name and payload (Event::Tick(3)), even when imported via a library alias. A variant the enum does not declare (Event::Tik) is a type error fromhale checkwherever it is written, a value, a constructor call or amatchpattern, with the nearest variant suggested. - Exhaustive matching:
matcharms can match literal sub-patterns (Event::Tick(0)) before variable bindings (Event::Tick(n)), and must cover every variant in theenum. enumvs.fallible(E): Useenumfor domain states and sum-type messages; usefallible(E)(When a Call Can Fail) for functions that can fail.
Names, Parsing and the Variant List
Section titled “Names, Parsing and the Variant List”Don’t write a name function and a parse function for each enum. The compiler writes four for every enum E:
type Lifecycle = enum { Active, Archived };
fn load(status: String) -> Lifecycle { // an unknown name fails with ParseError { kind: "unknown_variant", input } return Lifecycle::parse(status) or Lifecycle::Active;}
fn main() { for l in Lifecycle::variants { // [Lifecycle; 2], declaration order println(Lifecycle::index(l), " ", Lifecycle::name(l)); // 0 Active, 1 Archived }}E::name(v)is the variant’s external name, its values ignored.E::index(v)is its position in the declaration, from 0. Both work on any enum but a generic one.E::parse(s)is the variantsnames exactly, and fails withParseError { kind: "unknown_variant", input: s }otherwise; writeor raise, orora default.E::variantsis a value (no parentheses) holding every variant in declaration order. Both need variants that carry no values: on an enum likeEventabove,hale checkrefuses them and names the variant that carries values.- One external name: a variant’s name is its
json:tag’s when it has one (Sell `json:"sell"`), soname,parseand the JSON codecs always agree. With a catch-all,Other(String) `json:",other"`,parsecannot fail: an unknown name becomesOther(s), andname(Other(s))iss.E::variantsleaves that catch-all out. @wire_case: spell every variant in one case without tagging each:@wire_case(snake) type Phase = enum { InProgress, Done };makes the namesin_progressanddone. The cases aresnake,screaming_snake(IN_PROGRESS),kebab(in-progress) andlower(inprogress), and a variant’s ownjson:tag still wins.- Printing is unchanged:
println(Lifecycle::Archived)still showsLifecycle::Archived; ask forLifecycle::name(v)where you want the external name. To change how a record or a variant reads to people, give it a@display("…")template; see Strings & Text. The external name is the wire’s and the template is the reader’s, and neither changes the other. - A variant named
namekeeps its meaning: if an enum declares a variant calledname,parse,variantsorindex,E::nameis that variant, and the other three are still the enum’s.
Set Types (A | B | C)
Section titled “Set Types (A | B | C)”When each alternative is already a standalone type record or enum, declare a set type using | instead of wrapping each record in a redundant enum variant:
type Book { id: Int; title: String; }type Trade { qty: Int; px: Float; }type Unknown { raw: String; }type Inbound = Book | Trade | Unknown;
fn decode(n: Int) -> Inbound { if n == 0 { return Book { id: 7, title: "Kim" }; } if n == 1 { return Trade { qty: 2, px: 1.5 }; } return Unknown { raw: "?" };}
fn describe(m: Inbound) -> String { return match m { Book(b) -> "book " + b.title, Trade(t) -> "trade of " + to_string(t.qty), Unknown(u) -> "unknown " + u.raw, };}- Type-as-tag coercion: A
Book,Trade, orUnknownvalue automatically coerces toInboundwhereverInboundis expected (return values, a function body’s final expression included, function arguments, typedletbindings, or struct fields), and on the error channel of afallible(Inbound)function:fail,or fail errandor raiseof a member are the set’s (Converting Errors foror raise). - Bare names are for sets only: A set’s
matchnames a member by its type alone (Book(b)). Anenum’smatchalways names the enum (Shape::Line(l));Line(l)against an enum, or against a generic fn’s type parameter, is a type error. - Mixed branches are not joined into a set: The arms of an
iformatchwhose values are different members (Book { ... }in one arm,Trade { ... }in the other) are not joined intoInbound, even under an annotated binding (let m: Inbound = if ...) or as the value of areturnfrom a function declared-> Inbound:hale checkrefuses the expression (“if-expression arms have mismatched types”). Return from each branch instead (if n == 0 { return Book { ... }; } return Trade { ... };), or give each arm the set’s type, a call to a function returningInbound(let m = if n == 0 { book() } else { trade() };). - Members must be declared structs or enums: Primitive types like
Intcannot appear directly in a set; wrap them in a struct (type Count { n: Int; }). - Wire representation and seams: Across JSON APIs, a set serializes as a tagged object keyed by the member’s type name (
{"Trade": {"qty": 2, "px": 1.5}}). At the service layer, a seam can dispatch each member of a set to a dedicated handler method and verify exhaustiveness at compile time.
Generic Records
Section titled “Generic Records”A record can take type parameters (type Envelope<T> { data: T; request_id: String = ""; }), and so can a locus or a fn. See Generics.
Next: Generics.