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


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

In Fuzion, functions are purely syntactic sugar. Depending on the presence of a function result, functions are features that inherit from feature Function or Routine from the fuzion standard library.

Function Types

Function types are introduced by the keyword fun followed by a list of comma-separated argument types enclosed in parentheses and an optional result. The list of argument types can be empty, but the parentheses are always required.

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 { }

Function Values using Calls

Alternatively to inline code, function values could be defined as a call to an existing feature. In this case, a call without argument list is given after the keyword fun as in

  f := fun target.feat
which will be translated to
  internal_field := target
  f := fun (a A, b B, ...) => internal_field.feat a b ...

Here is the previous example using features double, squared and pow.calc as function values: