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.
Let us start by declaring a simple feature
myFirstFeature as follows:
When running this example, the feature
myFirstFeature will be called, which results
in execution of the call
say which prints a small
Feature declarations can be nested. The following example nests the
myFirstFeature from the previous example in the new
In this case,
example is called the outer feature,
myFirstFeature is an inner feature of
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
myFirstFeature, while this feature is never called
in this example, so the call to
say 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 perform a call, it is not required to provide 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
unit, which is why the last statement in the previous
unit. Without this explicit result value, the result
would be the instance of
myFirstFeature produced by the last
statement, which would be flagged as an error by the compiler.
In contrast, function features produce a result of an explicit type.
The next example defines a feature
message that results in
string. This feature is called and passed as an argument to a
say, which prints it:
say itself is a function feature that returns an instance of
unit, so there is no need for an explicit
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
return keyword in Fuzion.
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
is deduces its
result type from the last statement of its code.
The previous example can hence be simplified to the following equivalent code: