Skip to content

Types

These types are built into the language and need no import.

TypeSee
int int8 int16 int32 int64Numerics
uint uint8 uint16 uint32 uint64 uintptrNumerics
byte runeNumerics
float32 float64 complex64 complex128Numerics
boolBooleans
stringStrings
Option<T>Option
Result<T, E>Result
Partial<T, E>Partial
Slice<T>Slice
Array<T, N>Array
Map<K, V>Map
Ref<T>Ref
NeverNever
()Unit
(A, B)Tuples

A type annotation states a type explicitly. Most types are inferred from the value. Annotations are placed after : on a binding or field.

let empty: Slice<int> = []
binding annotation
struct Point {
x: float64,
field annotation
y: float64,
}

Return type annotations are placed after ->.

return type annotation
fn origin() -> Point {
Point { x: 0.0, y: 0.0 }
}

A type parameter is a placeholder for a type. It is enclosed in angle brackets after the name of a function, method, struct, enum, or alias.

fn first<T>(xs: Slice<T>) -> Option<T>
struct Pair<A, B> { left: A, right: B }
enum Tree<T> { Leaf(T), Node(Ref<Tree<T>>, Ref<Tree<T>>) }

Type arguments are given at the point of use, or inferred from the context.

let scores = Map.new<string, int>()given
let ages: Map<string, int> = Map.new()inferred from annotation

A bound T: Constraint limits which types a type parameter accepts.

interface Display {
fn to_string() -> string
}
T must satisfy Display
fn print_value<T: Display>(value: T) {
fmt.Println(value.to_string())
}

To give a type parameter multiple bounds:

fn render<T: Display + Shape>(value: T) -> string
T must satisfy both
Display and Shape

To give type parameters individual bounds:

fn label<T: Display, U: Ordered>(value: T, rank: U) -> string
T must satisfy Display
U must satisfy Ordered

To alias a type:

type UserId = int
type Handler = fn(Request) -> Response
type StringMap<V> = Map<string, V>

Type aliases are transparent, i.e. alternative names for the same type.

type UserId = int
let id: UserId = 42
let n: int = idUserId is just int

For distinct types, use a tuple struct:

struct UserId(int)
struct OrderId(int)
let user = UserId(1)
let order = OrderId(2)
let n: int = usererror: expected int, found UserId

Types never convert implicitly, not even between numeric types. The as operator converts between types explicitly.

let a: int = 1
let b: int64 = 2
let c = a + berror: cannot add int and int64
let c = a + (b as int)
explicit conversion

Numeric conversions:

let x: int = 42
let y = x as float64
let z = x as int8

Conversions of string to bytes or runes:

let s = "hello"
let bytes = s as Slice<byte>
let runes = s as Slice<rune>
let back = bytes as string

Conversions between incompatible types are disallowed.

let b = true
let n = b as interror: cannot convert bool to int

These types have a zero value:

TypeZero value
numeric0
string""
boolfalse
Option<T>None
Slice<T>[]
Array<T, N>N zero values of T
enum with defaultdefault variant

A struct or tuple has a zero value if all its component types have zero values.

struct Point { x: int, y: int }zero value is Point { x: 0, y: 0 }
type Pair = (int, string)zero value is (0, "")

See zero() and Zeroable