Skip to content

Values and types ​

Types ​

type(x)LiteralNotes
nilnilfalsy
booltrue falsefalse is falsy
int42 0xff 1_00064 bits; overflow gives a float
float1.5 1e964 bits
string"a" 'a'immutable, indexed by character
regex/re/iregular expressions
array[1, 2]mutable, passed by reference
dict{a: 1} {}keeps insertion order, by reference
functionfn f(x) { … } { it * 2 }functions
range1..5 1..<5lazy range
task—the result of calling an async fn (async)
mzs
type(42)  # → "int"
type(1..5)  # → "range"
is(1, "int")  # → true
is(1.0, "int")  # → false
is(1..5, "array")  # → true

Numbers ​

mzs
[42, 1_000_000, 0xff, 0b1010, 0o17]  # → [42,1000000,255,10,15]
0.1 + 0.2  # → 0.30000000000000004
7 / 2  # → 3
7.0 / 2  # → 3.5
9223372036854775807 + 1  # → 9223372036854776000.0

int / int divides to an integer. An int overflow does not wrap around, it turns into a float.

Strings ​

mzs
s = "world"
"hello, ${s}!"  # → "hello, world!"
'raw: \n'.len  # → 7
"привет".len  # → 6
"привет"[0]  # → "п"
"привет"[-1]  # → "т"
"привет"[1, 3]  # → "рив"
"abc"[10]  # → nil

Everything counts in characters, not bytes. More in Strings.

Arrays and dicts ​

mzs
xs = [1, "two", 3.0]
xs[1]  # → "two"
xs[-1]  # → 3.0

user = {name: "Ann", age: 30}
user["name"]  # → "Ann"
user.get("name")  # → "Ann"
user["city"]  # → nil

{name: …} and {"name": …} are the same dict. A dot reads only the fields of records; a plain dict uses d["k"]. A key from a variable goes in parentheses, a non-string key uses ->:

mzs
k = "id"
{(k): 7}  # → {"id":7}
{1 -> "A", 2 -> "B"}[1]  # → "A"

Key order is insertion order:

mzs
d = {b: 1, a: 2}
d["c"] = 3
d.json  # → "{\"b\":1,\"a\":2,\"c\":3}"
d.keys  # → ["b","a","c"]

Truthiness ​

Only nil and false are falsy. 0, "", [], {} are truthy.

mzs
[nil, false, 0, "", [], {}].map { bool(it) }  # → [false,false,true,true,true,true]
if 0 { "yes" } else { "no" }  # → "yes"

Check for emptiness explicitly: xs.empty, s == "".

Comparison ​

mzs
1 == 1.0  # → true
"2" == 2  # → false
[1, [2]] == [1, [2]]  # → true
{a: 1, b: 2} == {b: 2, a: 1}  # → true
"apple" <=> "banana"  # → -1
1 <=> "a"  # → nil

== compares by value and never fails. <, > between different types is an error:

mzs
1 < "2"
text
type: cannot compare int with string

Copying ​

Arrays and dicts are passed by reference. dup makes a shallow copy.

mzs
a = [1, 2]
b = a
b.push(3)
a  # → [1,2,3]

c = a.dup
c.push(4)
a  # → [1,2,3]

Records ​

record defines the shape of a dict: a constructor with fields and a type label. The value is still a dict.

mzs
record Money(amount, currency = "RUB")

m = Money(1500)
m  # → {"amount":1500,"currency":"RUB"}
m.amount  # → 1500
type(m)  # → "Money"
m.is("dict")  # → true
Money(currency = "USD", amount = 5).currency  # → "USD"

A field is written the same way it is read. A typo in a field name is a compile error:

mzs
record Money(amount, currency = "RUB")
m = Money(100)
m.amount += 50
m.amount  # → 150
m.merge({currency: "EUR"})  # → {"amount":150,"currency":"EUR"}
mzs
record Money(amount, currency = "RUB")
m = Money(100)
m.amout = 1
text
not a record field; did you mean '.amount'?

match matches a record by its name:

mzs
record Money(amount, currency = "RUB")

fn show(x) {
  match x {
    Money {amount, currency: "EUR"} -> "€${amount}"
    Money -> "${x.amount} ${x.currency}"
    else -> "not money"
  }
}
show(Money(5, "EUR"))  # → "€5"
show(Money(700))  # → "700 RUB"
show(42)  # → "not money"

Two records of the same type cannot be added with + (that would be a merge, not a sum) — use merge.