Files
Decodal/doc/manual/source/language/functions.md
T

42 lines
1.1 KiB
Markdown

# Functions
A function is a pure expression that accepts values and returns a value.
## Syntax
```dcdl
(value: Int) => value + 1
```
Use ordinary call syntax to invoke a function.
```dcdl
let
increment = (value: Int) => value + 1;
in
increment(41)
```
A function can have multiple parameters.
```dcdl
(input_a: { hoge = Int & >= 0; }, input_b: { fuga = Int; }) =>
{
hoge = input_a.hoge;
fuga = input_b.fuga;
}
```
A parameter range is optional.
When present, the argument is validated and refined using the same rules as `as` when the argument is referenced.
## Scope and evaluation
Functions have lexical scope and can reference bindings from where they were defined.
Arguments are evaluated lazily, so an argument not referenced by the function body is not evaluated.
Recursive field or argument dependencies produce a cycle diagnostic.
Functions can be referenced and called as intermediate values, but cannot be materialized as data.
An unapplied function remaining in a materialization target produces a diagnostic.
Function equality is not defined, and composing two function values with `&` produces a conflict.