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

Feature Declaration

A feature is the most fundamental entity of Fuzion. A feature can be declared and can be called. Features are very powerful, they are designed to generalize fairly different concepts found in other programming languages such as functions, methods, constructors, classes, structs, records, packages, interfaces, fields, and local variables.

Feature Declaration

Let us start by declaring a first simple feature myFirstFeature as follows:







When running this example, the feature myFirstFeature will be called, which results in execution of the call fuzion.std.out.print that produces a small greeting.

Nested Features

Feature declarations can be nested, the following example nests the feature myFirstFeature from the previous example into a new feature example.







In this case, example is called the outer feature, while myFirstFeature is an inner feature of example.

If you have a background in Java or C#, the outer feature has the role of a package while the inner feature is a class within this package. But you might as well imagine the outer feature to be something like a class and the inner feature is a method in this class.

If you run this example, you will see no output. The reason is that the output is performed withing myFirstFeature, while this feature is never called in this example, so the call to stdout.println is not executed.

To see some output, we will have to add calls to the feature. The simplest way to call a feature is by giving its name. Several statements can be separated by semicolons. Here is the previous example with three calls to myFirstFeature added.







To perform a call, it is not required to provide parentheses ( ), they are optional if no arguments are passed to the call as in this example.

The features defined so far are routines. Routines are features declared using the keyword is followed by a sequence of statments or code enclosed with curly braces { }. This code is executed when this feature is called.

Features can produce a result that is returned when the the feature is called. The next example defines a feature message that results in a string that is then printed:







Fuzion is statically typed, so we have to declare the result type of a feature that returns a value. The result type string is given after the feature name. Any feature that produces a result implicitly has an inner field called result that has to contain the returned value after execution of the feature's code.

Fuzion's grammar makes extensive use of carefully chosen syntactic sugar. This allows for simplified syntax in most common cases to enhance clarity and readability by avoiding boilerplate code. We can immediately make use of two simplifications in our example: First, if there is no explicit assignment to the result variable, the last expression in the code of a routine will be automatically assigned to result, our example can hence be simplified to:







Next, type inference can be used to avoid giving the result type explicitly if this type can be deduced from the result value. A routine that produces a result can make use of type inference for the result type by using => followed by an expression that produces the result instead of the code block defined with curly braces { }:









next: Fields