2. Values, types and functions
let binds immutably, var mutably. The primitive types are Int, Float, Bool
and String. A top-level function has to write out every parameter type and its return
type — the signature is the contract.
fn square(x: Int) -> Int = x * x
fn abs(x: Int) -> Int =
if x < 0 { 0 - x } else { x }
pub fn main() -> Unit !io = {
var total = 0
total = total + square(3)
total = total + abs(-4)
println(to_string(total))
}
Open in playground
13
The pipe |> puts its left-hand side into the first argument of the call on its right,
so a line reads in the direction the data flows:
fn double(x: Int) -> Int = x * 2
fn inc(x: Int) -> Int = x + 1
pub fn main() -> Unit !io =
5 |> double |> inc |> to_string |> println
Open in playground
11