Named types
Most of a program's types are named types: a built-in representation, a name that says what the value means, and the rules a valid value must follow.
type Stock = Int { min: 0}type Quantity = Int { min: 1 max: 1000}type ProductName = Text { min_length: 1 max_length: 120}type OrderId = Uuid {}
OrderId has no rules, but it is still its own type: an OrderId can't be
passed where another UUID-based type is expected. Every type declaration
creates a new type. There are no aliases, names that are just another way of
writing an existing type.
A named type can build on another named type and add rules. The new type keeps every rule of the one it builds on, so it can be used wherever that type is expected. The other way round needs a construction, which checks the extra rules.
type ProductName = Text { min_length: 1 max_length: 120}type ShortProductName = ProductName { max_length: 40}function catalogue_name(name: ShortProductName) -> ProductName { return name}
Constraints
| Constraint | For | Value | Meaning |
|---|---|---|---|
min, max |
Int, Decimal |
number | Inclusive bounds |
min_length, max_length |
Text, Bytes, List<T>, Set<T>, Map<K, V> |
whole number | Inclusive bounds; text counts Unicode characters, collections count items |
pattern |
Text |
string | A regular expression the text must match |
format |
Text |
email |
The text must be an email address |
dimensions |
a type built on Vector |
whole number, 1 to 16,000 | Exactly that many items (AI types) |
distance |
a type built on Vector |
a VectorDistance: cosine (the default), euclidean or inner_product |
How its vectors are compared: ai.distance and its index follow it (Storing a vector) |
Each constraint is written name: value, one per line. There are no other
constraints and no custom validation functions.
A refinement, a type that builds on another, can only add to the rules it inherits. These are errors:
type.constraint_contradiction: amaxbelow an inheritedmin, or the other way round, and the same for lengths and durations. It is also aDecimalrefinement whosescalediffers from the one it inherits, since the scale decides how the value is stored and sent; a currency other than the one it inherits, in aMoneytype'scurrency:or a rate type'sfrom:andto:; and a vector type'sdimensions:other than the one it inherits or outside its lengths.type.constraint_loosened: a bound looser than the one it inherits, such asmax_length: 50on a type withmax_length: 40.
The same rules hold for the settings on the fields of every type with
fields, entities and events included. The settings that only an entity's or
an event's fields take (generated, on_delete, unique, immutable,
role, personal, erase, retention) are described with those
features, starting at Data.
An exact number holds at most 38 digits, and a number literal with more is
type.invalid_literal.
Markdown for agents: /docs/named-types.md.Built from Jadpo main at a707efb.