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

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 simple feature my_first_feature as follows:

When running this example, the feature my_first_feature will be called, which results in execution of the call say which prints a small greeting.

Nested Features

Feature declarations can be nested. The following example nests the feature my_first_feature from the previous example in the new feature example.

In this case, example is called the outer feature, while my_first_feature 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 think of the outer feature as a class and the inner feature as a method in this class.

If you run this example, you will see no output. The reason is that the output is performed within my_first_feature, while this feature is never called in this example, so the call to say is not executed.

Calling Features

To see some output, we will have to add calls to the feature. Several calls can be separated by semicolons. Here is the previous example with three calls to my_first_feature added.

To perform a call, it is not required to add parentheses ( ), parentheses in calls are optional.

Constructor Features and Function Features

The features we have seen so far are constructor features, i.e., they define a type and the result of a call to these features is an instance of this type. The last statement in a construction feature must produce a value of type unit, which is why the last statement in the previous example is unit. Without this explicit result value, the result would be the instance of my_first_feature produced by the last statement, which flags an error in the compiler.

In contrast, function features produce a result of an explicit type. The next example defines a feature message that results in a String. This feature is called and passed as an argument to say, which prints it:

say itself is a function feature that returns an instance of type unit, so there is no need for an explicit unit value at the end here.

Fuzion is statically typed, so we have to declare the result type of a function feature. The result type, String in our example, is given after the feature name. The value returned by a function feature is the value produced by the last statement of the feature. There is nothing like a return keyword in Fuzion.

Routines

Features that contain code are called routines. Routines fall in two categories, constructor features and function features.

Type Inference for Routines

In many case, the result type of a function feature can be derived automatically from the value of the last statement, such that providing the type explicitly would be redundant. In Fuzion, type inference can be used to avoid giving the result type explicitly if this type can be inferred from the result value. A function feature declared using => instead of the explicit result type followed by => infers its result type from the last statement of its code.

The previous example can hence be simplified to the following equivalent code: