Fuzion Logo
flang.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. 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 or argument types can be empty, but the parentheses are always required.

Function Values using Inline Code

Function values are expressions starting with the keyword fun and followed by a feature declaration without a feature name. The following expressions give valid function values:

  fun (x i32) bool is i>=0         // a function with i32 argument that returns a bool
  fun String is "Hello"            // a function without argument that returns a String
  fun unit is out.print("Hello")   // a function value that prints Hello.
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.

Next, the result type in expressions such as

  fun (x i32) i32 is 2*x

can be inferred automatically using => similar to the result type inference in feature declarations:

  fun (x i32) => 2*x

The simplified example hence becomes







Further simplification with more syntactic sugar may be possible, but is not implemented at this point. Here is an example of what this could look like:







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: