Reference · 02
Type System
Complete reference for Ochre's static type system — primitives, generics, Option/Result, Task/Channel, function types, deep immutability, and dynamic JSON interop.
Ochre checks types statically in ochre-types before execution or code generation.
Built-in Types
| Syntax | Description |
|---|---|
Int | 64-bit signed integer |
Float | 64-bit IEEE-754 floating-point number |
Bool | Boolean (true / false) |
Str | UTF-8 string |
Void | Unit type for statements / functions without a return value |
List<T> | Homogeneous Copy-on-Write dynamic array |
Map<K, V> | Homogeneous Copy-on-Write ordered map; indexing returns V? |
T? | Option<T> (Some(T) or None) |
T!E | Result<T, E> (Ok(T) or Err(E)) |
Task<T> | Handle returned by spawn { ... }; join(task) yields T |
Channel<T> | Typed thread-safe queue; send(ch, T) and recv(ch) -> T? |
StructName | User-defined nominal struct type |
EnumName | User-defined nominal tagged enum type |
fn(P1, ...) -> R | Function / closure type |
Polymorphic Built-ins & Anti-Bypass Enforcement
In v0.1.0-alpha.4, polymorphic built-in functions (push, map, filter, reduce, len, contains, keys, send, recv, close, join, http_serve, int, float) are tracked as first-class Type::Builtin values. This guarantees type safety even when:
- A built-in is aliased to a variable (
let f = push; f([1, 2], "x")is rejected). - A collection starts empty with an explicit annotation (
let xs: List<Int> = []; push(xs, "x")is rejected). - An unannotated closure is passed to
map,filter,reduce, orhttp_serve(parameter types are inferred from the collection or request type).
Task<T> Inference Rules
For spawn { ... }:
- Both explicit
return exprstatements and a trailing tail expression contribute to the inferredT. - If all control-flow paths return compatible types,
spawninfersTask<T>. - If two return paths produce incompatible types (e.g.,
IntvsStr), the checker emits a compile-time error. - If one branch returns a value (
Int) while another path falls through without returning (Void), the checker emits a compile-time error. - Nested
spawnblocks maintain isolated return contexts.
Deep Immutability (let vs var)
letbindings disallow reassignment and nested field/index mutations (xs[0] = 1,rows[0][1] = 2,p.x = 3).varbindings allow reassignment and nested mutation, with Copy-on-Write isolation for any aliased snapshots.- Assigning into a non-variable temporary expression (
[1, 2][0] = 9) is rejected at compile time.
Dynamic JSON Boundary (json_parse)
Because JSON schemas are not known at compile time, json_parse(raw: Str) returns Any!Str. When unwrapped (let data = json_parse(raw) ?? #{}), dynamic JSON values can be indexed and bridged into typed variables; type mismatches on dynamic values are checked at runtime by the VM.