Skip to content

0.8.0 – `let`, tuples, and patterns

Latest
Compare
Choose a tag to compare
@liquidev liquidev released this 30 Oct 21:34
242849b

#129 let [docs]

A let keyword was introduced to separate variable declarations from assignments to existing variables. This is to improve local reasoning about code.

# Before:
a = 1
  
# After:
let a = 1

#22 Tuples and records [docs]

Tuples aggregate values in an ordered, heterogenous, immutable way:

let tuple = (1, 2)
assert(tuple._0 == 1)
assert(tuple._1 == 2)

Tuples can be destructured using let:

let (x, y) = (1, 2)
assert(x == 1 and y == 2)

Records are similar to tuples, but are unordered:

let position = { x: 1, y: 2 }
assert(position.x == 1)
assert(position.y == 2)
assert(position == { y: 2, x: 1 })  # swapping around the keys - the record is still the same

and can also be destructured with let expressions:

let { x, y } = player.position
# or rename the fields:
let { x: player_x, y: player_y } = player.position

By default, the record must match exactly. This can be relaxed by specifying a .. "rest" token at the end:

let { something, .. } = some_library
something(1, 2, 3)

Tuples and records are compared by value.

#148 _ pattern by @grisenti [docs]

_ is now a keyword and is used for discarding values.

let _ = some_function()
print(_)  # error

This feature is mostly useful in conjunction with the new tuple and record patterns, to ignore fields:

let (x, _) = (1, 2)
let { x: _, y } = { x: 1, y: 2 }