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

Functions

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

In Fuzion, functions are purely syntactic sugar. Depending on the presence of a function result, functions are features that inherit from 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

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

  fun (x i32) bool { result = i>=0; } // a function with i32 argument that returns a bool
  fun String { "Hello" }              // a function without argument that returns a String
  fun { 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 as f(x) since the compiler inserts the call if a field of function type is followed by a list of actual arguments starting with (.

Next, the result type in expressions such as

  fun (x i32) i32 { result = 2*x; }

can be inferred automatically when the 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 into
  internal_field := target;
  f := fun (a A, b B, ...) => internal_target_field.feat(a,b,...);

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







TBW: 2.infix * does not work yet. need to check why.


next: Tuples