Repository navigation
Functions and Calls
Functions use ordinary shell-style calls while retaining typed arguments and results. Printed output is independent of the result returned to the caller.
fn area($width: Float, $height: Float): Float { $width * $height }
let $size = area 3 2.5
echo $size (area 2 4)
7.5 8.0
Parameters and results can be annotated. Integer arguments widen where Float is
required. Other conversions are explicit. A final expression supplies the result;
return value supplies it early. A trailing newline or semicolon does not discard
that result. Bare return and an empty body produce ().
Omitting the return annotation means : (): the function must produce unit.
Use : any for a function whose result may have any type, or declare the required
result type. This rule also applies to methods, anonymous functions, and
function prototypes. Unannotated parameters still
accept any value.
fn announce($text: String)
{
echo $text
()
}
fn identity($value): any { $value }
fn answer(): Integer { 42 }
echo (announce "Ready") (identity "memo") (answer)
Ready
() memo 42
A final command can return ExecResult. The explicit () above ends the function
with unit after printing. The output text is separate from the function’s value.
A known incompatible result is rejected before execution. Results whose type is
only known at runtime are checked when the function returns. return value
obeys the same contract as a trailing expression.
| Form | Meaning |
|---|---|
work |
Call without arguments |
let $x = work |
Call; unresolved command is a runtime error |
let $x = "work" |
Store text |
echo work |
Pass the word as an argument |
echo (work) |
Call, then pass the result |
let $f = `work |
Validate and store a reference without calling |
$f 7 |
Call a stored reference with one argument |
Parentheses contain one expression, not a list of statements. Empty parentheses
in argument position supply unit: work () is a one-argument call.
fn ticket($name: String, $zone: String | ()): String
{
let $area = if $zone == () { "general" } else { $zone }
$name + ": " + $area
}
fn total($values: Integer...): Number { $values.sum }
echo (ticket "Mira") (total 4 6 8)
Mira: general 18
Trailing T | () parameters may be omitted from right to left. A required
parameter cannot follow an omittable one. A final $values: T... collects extra
arguments into [T]; it receives an empty array when no extras remain.
Use ... to expand an existing array into individual arguments.
Backticks validate at runtime. A missing target, inaccessible module function, or non-executable file is an error at reference creation. Scripted function references retain their selected version. Later redefinitions affect newly compiled code, not an existing reference. External names remain resolved through PATH when called.
Known compiled calls preserve the definitions they selected too. Forward functions are registered for the submitted source before execution. Anonymous functions add live captured variables; named functions otherwise use active caller-variable scope.
Related: Closures and function types · Commands
Begin here
Values and expressions
- Language basics
- Variables and scope
- Numbers and logic
- Strings and paths
- Arrays
- Hash maps
- Ranges and iteration
- Control flow
- Match and type guards
Functions and types
- Functions and calls
- Closures and function types
- Methods and properties
- Structs
- Enums
- Named types and unions
- Conversions
- Types and properties
Shell and modules
- Pipelines
- Pipeline splits
- Pipeline formats
- Commands and exit status
- Redirection
- Environment and exported variables
- Modules and visibility
- Nested modules and directory packages
- Conditional imports and declarations
- Errors and cleanup
- Supervised processes and terminals
Library guides
- Standard library
- The implicit prelude
- Bytes, files, readers, and writers
- Explicit item streams
- JSON documents and typed mapping
- Sockets, DNS, and TLS
- HTTP clients
- HTTP servers
- Framing, server-sent events, and JSON-RPC
- Terminal colors, styles, and geometry
- Build an application console
Interactive use