Declarations
Structs
export struct User {
name: Str
age: Int
address: Address?
}
- Fields are immutable. There are no private fields; construction across packages must be able to fill every field.
- Construction uses named arguments and requires every field:
User(name: "Ada", age: 36, address: None). Order is free. - There is no constructor, inheritance,
override, orabstract. - An empty struct is valid and useful as a marker:
export struct Unit {}. - At most one field may be
embed; see Types.
Enums
export enum Role {
Admin
Member(Int)
Guest
}
- A variant may have no payload or one or more payload types.
- An enum without payloads is a typed constant.
- Enums have no fields. Behavior lives in package-level functions that take the enum as a parameter; receiver sugar works (
role.label()≡label(role)). - Variants are referenced as
Role.Member(3), or with a qualifier from another package (models.Role.Member(3)).
Functions and receivers
A receiver function is an ordinary function whose first parameter has a struct or enum type. There is no self and no marker.
export fn add(c: Catalog, b: Book) -> Catalog {
Catalog(books: c.books.push(b))
}
// c.add(b) is add(c, b)
Rules:
- The sugar
value.f(args)applies only when a functionfexists in the package where the value’s type is declared. There is no dynamic dispatch. - A receiver function must live in the same package as its receiver type.
- Overloading is allowed only when the receiver type differs. Overloading by name or arity is forbidden.
- Parameters have nominal or primitive types. No varargs and no default values.
- A function whose parameters are not struct-typed is an ordinary function: call
empty(), notcatalog.empty().
Package-level bindings
package money {
export let default_currency: Str = "IDR"
let lookup: Map<Str, Int> = map.empty()
}
Bindings are immutable, eager, and parameterless. They may refer to other bindings in the same package as long as no cycle forms. export exposes them as money.default_currency or through destructuring.
Tests
test "formats with currency" {
let p = Price(amount: 1250, currency: "IDR")
expect.eq(format_price(p), "12.50 IDR")
}
A test is a package item. Titles are unique per package. Tests are not emitted by lt build; only lt test produces native test files. A test body is a normal statement block with no parameters and no return type, and it may use private declarations.
Complete example
package receiver {
export struct Counter {
value: Int
}
export fn bump(c: Counter) -> Counter {
Counter(value: c.value + 1)
}
test "calls as a function" {
expect.eq(bump(Counter(value: 1)), Counter(value: 2))
}
test "calls as a method" {
expect.eq(Counter(value: 1).bump(), Counter(value: 2))
}
}