Types
These types are built into the language and need no import.
| Type | See |
|---|---|
int int8 int16 int32 int64 | Numerics |
uint uint8 uint16 uint32 uint64 uintptr | Numerics |
byte rune | Numerics |
float32 float64 complex64 complex128 | Numerics |
bool | Booleans |
string | Strings |
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 |
Never | Never |
() | Unit |
(A, B) | Tuples |
Type annotations
Section titled “Type annotations”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 annotationfn origin() -> Point { Point { x: 0.0, y: 0.0 }}Type parameters
Section titled “Type parameters”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>()givenlet ages: Map<string, int> = Map.new()inferred from annotationBounds
Section titled “Bounds”A bound T: Constraint limits which types a type parameter accepts.
interface Display { fn to_string() -> string}T must satisfy Displayfn 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) -> stringT must satisfy both
Display and ShapeTo give type parameters individual bounds:
fn label<T: Display, U: Ordered>(value: T, rank: U) -> stringT must satisfy Display
U must satisfy OrderedType aliases
Section titled “Type aliases”To alias a type:
type UserId = inttype Handler = fn(Request) -> Responsetype StringMap<V> = Map<string, V>Type aliases are transparent, i.e. alternative names for the same type.
type UserId = int
let id: UserId = 42let n: int = idUserId is just intFor 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 UserIdType conversion
Section titled “Type conversion”Types never convert implicitly, not even between numeric types. The as operator converts between types explicitly.
let a: int = 1let b: int64 = 2let c = a + berror: cannot add int and int64let c = a + (b as int)explicit conversionNumeric conversions:
let x: int = 42let y = x as float64let z = x as int8Conversions 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 stringConversions between incompatible types are disallowed.
let b = truelet n = b as interror: cannot convert bool to intTypes with zero values
Section titled “Types with zero values”These types have a zero value:
| Type | Zero value |
|---|---|
| numeric | 0 |
string | "" |
bool | false |
Option<T> | None |
Slice<T> | [] |
Array<T, N> | N zero values of T |
| enum with default | default 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, "")