4-1: Functions
Functions allow BushelScript code to be named, reused, and called recursively. Put another way, they allow commands to be defined and implemented locally, in BushelScript code (as opposed to remotely, like in apps).
For background on the design of the command model, including functions, please consult this blog post.
See also: Quick Tutorial.
Named functions#
A named function definition consists of:
- One of the keywords
on
orto
- A name, which gets defined as a command term in the current dictionary if such a term isn't already there
- A list of parameter definitions, one per line, which each consist of:
- A name, which gets defined as a parameter term in the command's dictionary if such a term isn't already there
- An optional bracketed URI for the parameter term, or one of the keywords
direct
ortarget
; if absent, defaults to a URI derived from the parameter name- This can be used to define direct and target parameters, or to provide synonym parameter names
- An optional variable name to refer to the parameter's value in the function body; if absent, defaults to the parameter's name
- A
type
, used by local function selection to determine which function to call given a set of arguments
- The keyword
do
to end the parameter list, followed by a line break - A sequence of expressions in the function body, which may use any command, parameter and variable terms just defined
- The keyword
end
to end the function body
e.g.,
Anonymous functions#
An anonymous function definition consists of:
- Optionally:
- The keyword
take
- A comma-separated list of variable names to bind ordered parameters to
- The keyword
- The keyword
do
- One of:
- Sequence body:
- A line break
- A sequence of expressions in the function body, which may use any variable terms just defined
- The keyword
end
- Non-sequence body:
- A single expression for the function body, which may use any variable terms just defined
- Sequence body:
e.g.,