21. Type Aliases
21.1 Concrete Type Aliases
Use type to create a transparent alias for an existing type. The alias and the original type are fully interchangeable.
-- Alias for a primitive type
type Natural = Int;
let n Natural = 42;
-- Alias for a collection type
type IntList = List<Int>;
let xs IntList = List(1, 2, 3);
-- Alias for a tuple type
type Point = (Float, Float);
let p Point = (1.0, 2.0);
-- Alias for a function type
type IntToInt = (Int) Int;
fn apply(f IntToInt, x Int) Int = f(x);
-- Alias for a struct type
struct Vec2 { x Float, y Float }
type Vector = Vec2;
let v = Vector { x: 1.0, y: 2.0 };
-- Alias for a template struct instantiation
struct Box<T> { value T }
type IntBox = Box<Int>;
let b IntBox = Box { 42 };
let b2 Box<Int> = b; -- alias is transparent
21.2 Generic Type Aliases
Type aliases can have type parameters. They are expanded when type arguments are provided.
-- Generic alias for a tuple
type Pair<T> = (T, T);
let ip Pair<Int> = (1, 2);
let fp Pair<Float> = (3.14, 2.72);
-- Multiple type parameters
type Entry<K, V> = (K, V);
let e Entry<String, Int> = ("age", 30);
-- Generic alias for an array
type Vec<T> = [T];
let v Vec<Int> = [1, 2, 3];
-- Generic alias wrapping a template struct
type Wrapper<T> = Box<T>;
fn unwrap<T>(w Wrapper<T>) T = w.value;
21.3 Visibility
Type aliases can be marked private in modules to prevent them from being exported.
private type InternalId = Int;
type PublicId = Int; -- exported by default