Function
A function definition provides a way to not repeat yourself in other definitions. We can define functions in several places and then use them in an expression or action. This way, we only need to define the logic for something once.
Signature¶
A function's requires and returns may name an existing type
rather than spelling out an inline aggregation, which makes unary and nullary
functions natural to write:
function CalculateTotal is {
requires record TotalInputs
returns Price
return call function Tax.Apply(subtotal)
}
Any type works, and the kind keyword is optional: requires Age,
requires type Age and requires record Args are all valid.
The inline aggregation form is deprecated
It continues to parse and validate but emits a
[deprecated] message.
Name a type instead.
Functions Must Be Pure¶
A function body may not write entity state (set, morph, become), nor
send, tell or yield. This is enforced at parse time, so an effect
statement can never enter a function's AST at all.
What remains legal is refusal (require, error) and pure computation
(let, when, match, foreach, do, return, embedded code blocks).
Purity is what makes a function safe to call from anywhere — a handler body
or another function — with no scope gate and no ordering concern.
Calling a Function¶
call is a value expression rather than a bare statement, because
a pure function's result would otherwise be discarded:
let total = call function Pricing.CalculateTotal(subtotal, taxRate = rate)
set field grandTotal to call function Tax.Apply(total)
Arguments are positional first, then named, and are bound to the function's
requires fields. Calling a function that declares no returns is an
Error.
Occurs In¶
Contains¶
A Function is not a Processor
A Function extends the vital definition base rather than Processor, so it bears no ports, no version and no copyright.