Values and types
Types
type(x) | Literal | Notes |
|---|---|---|
nil | nil | falsy |
bool | true false | false is falsy |
int | 42 0xff 1_000 | 64 bits; overflow gives a float |
float | 1.5 1e9 | 64 bits |
string | "a" 'a' | immutable, indexed by character |
regex | /re/i | regular expressions |
array | [1, 2] | mutable, passed by reference |
dict | {a: 1} {} | keeps insertion order, by reference |
function | fn f(x) { … } { it * 2 } | functions |
range | 1..5 1..<5 | lazy range |
task | — | the result of calling an async fn (async) |
type(42) # → "int"
type(1..5) # → "range"
is(1, "int") # → true
is(1.0, "int") # → false
is(1..5, "array") # → trueNumbers
[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.0int / int divides to an integer. An int overflow does not wrap around, it turns into a float.
Strings
s = "world"
"hello, ${s}!" # → "hello, world!"
'raw: \n'.len # → 7
"привет".len # → 6
"привет"[0] # → "п"
"привет"[-1] # → "т"
"привет"[1, 3] # → "рив"
"abc"[10] # → nilEverything counts in characters, not bytes. More in Strings.
Arrays and dicts
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 ->:
k = "id"
{(k): 7} # → {"id":7}
{1 -> "A", 2 -> "B"}[1] # → "A"Key order is insertion order:
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.
[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
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:
1 < "2"type: cannot compare int with stringCopying
Arrays and dicts are passed by reference. dup makes a shallow copy.
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.
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:
record Money(amount, currency = "RUB")
m = Money(100)
m.amount += 50
m.amount # → 150
m.merge({currency: "EUR"}) # → {"amount":150,"currency":"EUR"}record Money(amount, currency = "RUB")
m = Money(100)
m.amout = 1not a record field; did you mean '.amount'?match matches a record by its name:
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.