Skip to content

Type Aliases

A type alias gives an existing type a second name with type Name = T. Unlike a newtype, an alias is transparenttype Meters = float is interchangeable with float, so it documents intent without adding a distinct type:

type Meters = float and friends — transparent aliases for clearer signatures.

11-type-aliases.zolo
Playground
// Feature: Type aliases — `type Name = T`
// Syntax: `type Alias = ExistingType`
// When to use: give a semantic name to an existing type without
// creating a new type. Aliases are TRANSPARENT — `type Meters = float`
// accepts any `float`. For an opaque type, see 12-newtypes.

// Numeric domains.
type Meters = float
type Seconds = float
type Velocity = float

fn speed(distance: Meters, time: Seconds) -> Velocity {
  return distance / time
}

let d: Meters = 100.0
let t: Seconds = 9.58
print(speed(d, t))  // ~10.44

// Alias for collections.
type IdList = [int]

fn mark_processed(ids: IdList) {
  print("processing {ids.len()} ids")
}

mark_processed([1, 2, 3])

// Aliases do NOT prevent mixing compatible types — they are just names.
let mixed: Meters = 9.58  // ok even though it conceptually is "time"
print(mixed)
// The typechecker does not distinguish Meters from Seconds.
// If you want that distinction, use `newtype` (next file).

For a distinct type the compiler keeps separate, see Newtypes.

See also

enespt-br