Fuzion Logo
fuzion-lang.dev — The Fuzion Language Portal
JavaScript seems to be disabled. Functionality is limited.

Functions

Functions are expressions that produce a value that can be called to produce another value.

In Fuzion, functions are purely syntactic sugar. Defining a function in Fuzion is done using lambda expressions. Depending on the number of arguments, functions are features that inherit from the Fuzion standard library features Function (general functions), Unary (functions with one argument, they can be composed), or Lazy (functions with no argument). Unary and lazy functions can be assigned to variables of type Function as well, if so desired.

Function Types

The type of functions is Function R A0 A1... where R is the result type and A0, A1, ... are the types of the arguments. The Unary and Lazy types work similarly, but Unary takes exactly one argument and Lazy takes exactly no argument types.

Function Values using Inline Code

Function values are expressions enclosed in parentheses followed by a list of arguments, then ->, and followed by a feature declaration without a feature name. The following expressions give valid function values:

(x -> x>=0)                // a function with i32 argument that returns a bool
(() -> "Hello")            // a function without argument that returns a String
(() -> out.print("Hello")) // a function value that prints Hello
(a, b -> a + b)            // a function that returns the sum of two arguments

The following code gives an example of a feature eval that receives an argument f of a function type and calls this with arguments 0, 1, 2, and 3.

Syntactic sugar allows to simplify this code: First, the call f.call x can be simplified to f x since the compiler inserts the call if a field of function type is followed by a list of actual arguments.

The simplified example hence becomes

The argument and result types in expressions such as

(x -> 2*x)

is inferred automatically from the type signature in features that accept a function as an argument:

eval(msg String, f i32 -> i32) is
last changed: 2024-06-28