Types
Types
Every declaration carries a type, written after the name and a colon.
let count: number = 0
let label: string = "ready"
let ready: bool = truePrimitives
| Type | Notes |
|---|---|
number |
The general numeric type, backed by a double. Used for ordinary arithmetic. |
bool |
true or false. |
string |
Text. |
i8, i32, i64 |
Sized signed integers, used where layout matters. |
f64 |
A sized float. |
number is a class with methods, not a raw machine value. The sized types are
the raw ones, and they appear in buffers, in FFI signatures, and anywhere the
in-memory representation has to be exact.
let initial: i64 = constant<i64>(8)
let total: number = new number(danger_cast<f64>(initial))Conversions
Conversions are explicit. There is no implicit numeric promotion.
constant<T>(value)produces a compile-time constant of a sized type.danger_cast<T>(value)converts between sized numeric types without a check. The name is a warning: it is for boundaries where the range is already known.value as Typeconverts between related object types, such as narrowing ajson::JsonValueto ajson::JsonObjectafter checking its kind.
let parsed: json::JsonValue = json::parse(text)
if parsed.kind() == "object" {
let object: json::JsonObject = parsed as json::JsonObject
}Composite types
| Written | Meaning |
|---|---|
Type? |
An optional. See the optionals page. |
Array<T> |
A dynamic array from std::collections. |
Map<K, V> |
A hash map from std::collections. |
pointer<T> |
A managed pointer. |
&T |
A reference to a slot, produced by methods like index_ref. |
(R)(A, B) |
A function type: returns R, takes A and B. |
closure(A, B) => R |
A closure type. |
Array and Map need no import. std::collections is auto-imported and its
types are used bare.
Collection literals
Arrays and maps have a shorthand, so a small collection needs no constructor:
let names = #["alpha", "beta", "gamma"]
let ports = #{"http": 80, "https": 443}An array literal is #[ with comma-separated values. A map literal is #{ with
key: value pairs. Element and value types are inferred from the contents, and
both produce the ordinary Array and Map types.