5. Functions and actions
Both pure functions and actions use the keyword def; the ! suffix on the name decides which it is. These are the first two of Hanki's three layers (§1, The three layers). The third layer, actors (§15), is built from actions.
A pure function has no side effects and may not call any action.
def sum(x: i32, y: i32) -> i32
x + y
end
An action may have side effects. Action names always end in !. The function type takes an optional effect annotation […] after the return type.
use io
def print_hello!(name: string) -> () [io]
io.print!("Hello, #{name}")
end
Rules:
- A
defwhose name does not end in!is a pure function. No effect annotation allowed. - A
defwhose name ends in!is an action. The effect annotation[…]is optional. An action that writes none still has the implicit[local](§6), which makes it non-substitutable and bars a puredeffrom calling it. - A pure
defcannot call any action. The!suffix at the call site is a visible reminder. - An action can call pure functions and other actions whose effects are a subset of its own.
- Types are mandatory on all parameters and the return type. No silent defaults.