Declarations and calls#
A function names its inputs, declares their types, and declares its return type. Use return in a multiline body. A one-line body is written => and returns its expression implicitly: fn double(value Int) Int => value * 2 (see suite rules
).
fn decrement(value Int) Int {
return value - 1
}
remaining := decrement(3)Parameters and return types are explicit; local annotations are optional.
Declarations and resolution#
- Named functions are module declarations or receiver methods. A
fninside a function body declares a local function: sugar for binding afnto the name (f := fn ...). It cannot call itself; module-level functions recurse. - Parameter names are unique and every parameter and result type is explicit, including
Void. - Signatures are collected before bodies are checked, so functions may call later declarations and recurse, including mutually.
- Duplicate module names and overloads are rejected. Generics follow generics .
- Parameters live in the body’s scope; a local cannot reuse a parameter name there. Branches, arms, loop bodies, and closures open nested scopes, which cannot reuse an enclosing local or parameter name.
The program entry#
An entry file declares a nongeneric fn main() with no parameters; export is optional. Its result is Void, Void!, Int?!, or Int. Void exits 0. Void! exits 0 on ok and, on an error, prints error: <message> to standard error and exits 1. Int?! does the same for an error, and otherwise exits with the Int in ok(n), or 0 for ok(null). Int is the exit status.
fn check() Void! {
print("checking")
}
fn main() Void! {
return check()
}Grouped parameters#
Names in a row share one type, as in fn reduce(amount, deduction Int). A mode written before the row (var, move or hold) covers every name in it: fn max[T Comparable](hold left, right T) &T holds both, and var a, b List[Int] changes both. To give one name a mode, write the names apart: hold left T, right T.
Argument names#
Labels are optional. An unlabeled argument fills the first parameter not yet filled, in declaration order. name: value fills that parameter in any order. A bare identifier is an ordinary expression, so renaming a local never changes which parameter it fills.
fn reduce(amount, deduction Int) Int {
return amount - deduction
}
remaining := reduce(10, 3)
explicit_remaining := reduce(amount: 10, deduction: 3)
mixed := reduce(deduction: 3, 10)
amount := 10
deduction := 3
swapped := reduce(deduction: deduction, amount: amount)
print(remaining)
print(explicit_remaining)
print(mixed)All four results are 7. Arguments resolve left to right and labels do not reserve parameters for later arguments:
reduce(10, amount: 3)suppliesamounttwice and fails.reduce(amount: 10, 3)suppliesdeductionpositionally.- Unknown labels, duplicate or missing arguments, and excess positional arguments are compile errors.
- Arguments never skip a parameter because it has a default.
- Values must be compatible with the parameter types under type compatibility ; labels never authorize conversions.
reduce(amount: amount, deduction: deduction) binds the same as reduce(amount, deduction). ms fmt --drop-labels removes name: name labels only when the call binds identically without them.
The rule covers functions, generics, methods, library APIs, and constructors. A call through a function value, including a copy of a named function in a variable, is positional (function values
). A method’s receiver is evaluated before its arguments and takes no positional slot. Constructor slots follow field declaration order. A zero-parameter function uses (). Function-type identity depends on ordered parameter types with their var markers and the result type; parameter names are not part of type identity
.
A parameter with a default may be omitted; others are required, except T?, which defaults to null. A supplied T is wrapped once as the present case (optional arguments
). Variadics and spreading are unsupported. Match bindings such as value => beside null => are patterns
, not calls.
Default arguments#
Write = expression after a parameter’s type to supply a default:
fn subtract(amount Int, deduction Int = 1) Int {
return amount - deduction
}
fn example() Int {
usual := subtract(5)
explicit := subtract(5, 2)
return usual + explicit
}usual is 4 and explicit is 3. A function whose sole parameter has a default also accepts function().
- Optional parameters come last: a parameter with a default, or of type
T?, may not precede a required one, and the checker rejectsfn choose(first Int = 1, second Int) Int. Constructor fields follow the same rule. Writefn choose(second Int, first Int = 1) Intand skipfirstby omitting it; to skip an earlier optional, label a later argument. - A default is checked against its parameter type in the function’s scope. It may use imports, module declarations, earlier parameters, and
self; current and later parameters are not in scope. Anonymous functions may use captures. - Evaluation: the callable and supplied arguments run first in written order, then omitted defaults run once each in declaration order, before the body. Defaults run afresh per call.
null,void,false, and zero are supplied values. - A failing default stops later defaults and the body.
?in a default propagates under the function’s result type. - Named functions, closures, and methods may declare defaults; function types and enum payloads cannot. Interface signatures permit only
= nullon Optional parameters. A concrete default wins; an interface view uses the interface’s. - Fields have their own defaults . Omitted arguments give no generic type evidence.
- Defaults are callable permissions of known callables. Inferred immutable copies preserve them; explicit function types, mutable callable bindings, and generic inference require every parameter. Defaults are not part of function-type identity.
Collections as arguments#
To accept a variable number of values, declare an ordinary list parameter:
fn sum(values List[Int]) Int {
total := values.sum()
return total
}
total := sum([1, 2, 3])
empty := sum([])
print(total)The argument is one list: sum(existing_values) or sum(values: existing_values). The parameter is required even for an empty list.
See collections for list literals and element requirements.
Evaluation order#
Evaluation follows source order; named arguments never reorder it.
- Evaluate the callable, including a method receiver, once.
- Evaluate supplied arguments left to right.
- Arrange values into declaration order, then evaluate omitted defaults before the body.
A failed or nonreturning argument prevents later arguments and the call. Constructor fields follow the same rule.
Void and Never#
Void is a value type with exactly one value, written void; it is not an uninhabited type (that is Never). A Void function may use a bare return or fall off the end. A function returning Void! (or Void!E) may do the same, and both return ok(), which you may also write explicitly. Other functions must return a compatible value on every reachable exit: a bare return or reachable fallthrough is an error. Multiline bodies need an explicit return.
import panic from "@std/core.ms"
fn check_positive(value Int) Void {
if not (value > 0) {
return panic(message: "check failed")
}
}
fn missing_value() Int {
return panic(message: "Required value is missing")
}panic has type fn(String) Never. A Never expression cannot complete normally and is accepted where any value type is expected. List[Never] is not compatible with List[Int].
Return completeness uses a conservative control-flow check:
| Construct | Reachable normal exits |
|---|---|
return or evaluation of Never | None |
if without else | Branch exits plus a path past the statement |
if with else or exhaustive match | The combined exits of all branches or arms |
for or while with a condition other than literal true | A possible exit, because the body may not execute |
Literal while true | Only a reachable break targeting that loop |
A nested function’s return or nested loop’s break cannot exit the enclosing one. No further constant folding applies.
An expression has no normal successor when a necessarily evaluated subexpression cannot complete: callable or receiver, arguments, constructor fields, list elements, map entries, interpolations, unary operands, and both operands of non-short-circuit operators. The right operand of and/or may be skipped, so it does not count; the left always does. Creating a function value does not run its body.
Typing is separate: 1 + panic(message: "stop") has type Int (operators
) but cannot complete, so the next statement is unreachable.
fn incomplete(active Bool) Int {
if active {
return 1
}
// Rejected: the false path reaches the end without returning Int.
}Unreachable statements are still parsed, resolved, and type-checked. A Never function has no reachable normal exit. A bare call may be a statement; other expressions cannot.
Calls that block#
A library call returning Promise[T] returns an operation whose Result is taken with .wait(). promise(() => ...) starts concurrent work; stream reads may defer work until the wait. Other operations return their result directly. There is no async or await: any function can wait. See promises
.
See function values and closures for functions as data.