Types and Values
Sentrie provides a robust type system that includes primitive types, collection types, and user-defined shapes. The type system is enhanced by a powerful constraint system that allows you to validate values against specific rules.
Primitive Types
Section titled “Primitive Types”Sentrie supports five fundamental primitive types:
| Type | Description |
|---|---|
number | Numeric values |
string | Text strings |
trinary | Trinary values (true / false / unknown) |
bool | Boolean values (true / false) - a special case of trinary |
document | Arbitrary JSON-like objects |
Declaring a Primitive Type
Section titled “Declaring a Primitive Type”let u: number = 50let u: number = 50.5let u: string = "hello"let u: bool = truelet u: document = { "name": "John", "age": 30 }Collection Types
Section titled “Collection Types”Collections allow you to work with groups of related values:
| Type | Description |
|---|---|
list[T] | Lists of type T |
map[T] | Maps with string keys and type T values |
record[T1, T2, ...] | Tuples with specific types |
Declaring a Collection Type
Section titled “Declaring a Collection Type”let u: list[number] = [1, 2, 3]let u: map[number] = { "one": 1, "two": 2, "three": 3 }let u: record[string, number, bool] = ["one", 1, true]Accessing Collection Elements
Section titled “Accessing Collection Elements”You can access collection elements using the [index] syntax. The index must be a string for maps and a number for lists and records.
let u: list[number] = [1, 2, 3]let first: number = u[0]let u: map[number] = { "one": 1, "two": 2, "three": 3 }let first: number = u["one"]For maps, you can also access elements using the . syntax.
let u: map[number] = { "one": 1, "two": 2, "three": 3 }let first: number = u.oneConverting Types
Section titled “Converting Types”You can convert between types using the cast .. as construct. The result is validated against the new type constraints before returning the result.
let u: number = cast "50" as numberlet u: string = cast 50 as stringlet u: bool = cast "true" as boollet u: document = cast { "name": "John", "age": 30 } as document