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

SyntaxDescription
Int64-bit signed integer
Float64-bit IEEE-754 floating-point number
BoolBoolean (true / false)
StrUTF-8 string
VoidUnit 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!EResult<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?
StructNameUser-defined nominal struct type
EnumNameUser-defined nominal tagged enum type
fn(P1, ...) -> RFunction / 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, or http_serve (parameter types are inferred from the collection or request type).

Task<T> Inference Rules

For spawn { ... }:

  1. Both explicit return expr statements and a trailing tail expression contribute to the inferred T.
  2. If all control-flow paths return compatible types, spawn infers Task<T>.
  3. If two return paths produce incompatible types (e.g., Int vs Str), the checker emits a compile-time error.
  4. If one branch returns a value (Int) while another path falls through without returning (Void), the checker emits a compile-time error.
  5. Nested spawn blocks maintain isolated return contexts.

Deep Immutability (let vs var)

  • let bindings disallow reassignment and nested field/index mutations (xs[0] = 1, rows[0][1] = 2, p.x = 3).
  • var bindings 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.