Types and values
The type names
| type | what a value of it is |
|---|---|
number |
a 64-bit signed integer (i64) |
float |
a 64-bit float (f64) |
string |
UTF-8 text |
boolean |
true or false |
list |
an ordered sequence; elements are any |
map |
string keys to any values |
option |
Some(value) or None |
result |
Ok(value) or Err(reason) |
any |
gradual: accepted anywhere, checked at run time |
void |
what a function returns when it returns nothing |
int and bool are still accepted as spellings of number and boolean, and
number? is sugar for option. The list above is the same list the parser
accepts, read out of the binary in the generated types reference.
Literals
42 -7 1_000
3.5 1.0 -0.25
"hello" "line\nbreak"
true false null
[1, 2, 3] {"title": "Dune", "pages": 412}
A number literal is an integer. 1 is a number and 1.0 is a float, and
they are different values with different types.
No implicit conversion
Nothing converts on its own, and the checker refuses the mix before anything runs:
---
midad: "0.1"
module: mixed
---
```exec
const ratio = 1.5;
print(str(ratio + 1));
```
× E0230: operator cannot combine float and numberThe fix is explicit: write 1.0, or convert with float(1).
Converting
| call | answers |
|---|---|
str(x) |
a string, for any value; a list or map prints as compact JSON |
int(x) |
a number, and refuses a string that is not a whole number |
float(x) |
a float |
int.parse(s) |
an option: Some(3) for "3", None for "3.5" or "x" |
float.parse(s) |
an option |
int_or(s, n) |
a number: the parsed value or the fallback |
float_or(s, n) |
a float: the parsed value or the fallback |
type_of(x) |
a string naming the value's runtime kind |
int("x") is a runtime error, not a zero. When the input might be bad, use
int.parse and handle the option, or int_or and take a default.
---
midad: "0.1"
module: convert
---
# convert
```exec
print(str(int("42") + 1));
print(str(int_or("not a number", 7)));
print(str(float("2.5")));
print(unwrap_or(int.parse("12"), 0));
print(str(is_none(int.parse("12.5"))));
print(type_of(42));
print(type_of(2.5));
print(type_of("x"));
print(type_of([1]));
print(type_of({"a": 1}));
print(type_of(Some(1)));
print(type_of(Ok(1)));
print(type_of(null));
```
43
7
2.5
12
true
int
float
string
list
map
option
result
nulltype_of names the runtime kind, and the names are not the type names: a
number reports int, a boolean reports bool. type_of on a wrapped value
answers option or result, because those do not carry their payload's type.
null reports null, which is the next section.
Lists and maps hold any
at(list, i) and get(map, k) answer any, not the type of what you put in.
A value read out of a collection therefore needs a cast before it is arithmetic
or a comparison:
---
midad: "0.1"
module: collections
---
# collections
```exec
const prices = [120, 45];
const first = at(prices, 0);
print(str(int(first) + 1));
const row = {"name": "ink", "cents": 1250};
print(str(int(get(row, "cents")) * 2));
print(str(get(row, "name")) + "!");
```
121
2500
ink!str(get(row, "name")) is needed for the same reason: without it the value is
any, and any + string is checked at run time rather than at check time.
None, and null
get on a map with no such key answers None, an empty option. null is a
value of its own and not another spelling of None: it prints null, type_of
answers null, is_null is true of it, and is_none(null) is a run-time
refusal (R0001, "expected option") rather than an answer.
---
midad: "0.1"
module: missing
---
# missing
```exec
const row = {"name": "ink"};
print(str(is_none(get(row, "cents"))));
print(str(is_null(get(row, "cents"))));
print(str(is_null(null)));
print(type_of(null));
print(null);
print(unwrap_or(get(row, "cents"), 0));
```
true
false
true
null
null
0is_none asks whether an option is empty, and it is the one you want after a
get. is_null asks whether a value is null: that is what a SQL NULL from
the database layer comes back as, and what json.parse makes of
a JSON null, which json.stringify writes back out as null.
The C backend compiles the null literal. A Cranelift build refuses it by name
("the null literal is not in the native subset yet; run with --backend c,
which compiles it"), so a program that uses it compiles to a native binary only
through C.
option and result
Some(x) and Ok(x) wrap a value; None and Err(reason) do not.
unwrap_or(v, fallback) answers the value or the fallback, and if let takes
the value out when it is there:
---
midad: "0.1"
module: option
---
# option
```exec
const present = Some("ink");
const absent = None;
print(unwrap_or(present, "none"));
print(unwrap_or(absent, "none"));
print(str(is_some(present)));
print(str(is_none(absent)));
if let Ok(n) = Ok(41) {
print(str(n + 1));
}
if let Err(e) = Err("no stock") {
print(e);
}
```
ink
none
true
true
42
no stock? inside a function that returns option unwraps a Some and returns
None early; inside a function that returns result it does the same with
Ok and Err. A ? on the wrong kind is a compile error.
The payload types are not tracked. option is option, whatever it holds, so
a hole that expects a number and gets a string is a runtime error rather than a
check-time one. This is one of the limits the language admits to.
Numbers
+, - and * on two numbers stay integers and are checked: an
overflow is a runtime error with a span, never a wrapped value and never a
panic.
---
midad: "0.1"
module: overflow
---
# overflow
```exec
let big = 9223372036854775807;
print(str(big + 1));
```
overflow.md:10:1
× R0001: integer overflow
╭─[overflow.md:10:1]
9 │ let big = 9223372036854775807;
10 │ print(str(big + 1));
· ──────────┬─────────
· ╰── integer overflow
11 │ ```
╰────/ is real division and always answers a float: 7 / 2 is 3.5. Integer
division is idiv(a, b), which floors, so idiv(-7, 2) is -4, and it refuses
a zero denominator the way / does. % is the remainder and truncates with the
quotient. The checker warns (W008) when it sees / on two integers that look
like they were meant to be integer division.
float arithmetic is not checked: overflow becomes inf. There is no integer
type wider than i64, no decimal type and no unsigned type.
What is not here
No structs, no records, no enums, no classes: a map with string keys is the
only record. No generics. No implicit conversion in either direction. No
nullable types in the type system: null exists and is any. The
limits page is the full list.