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


So far, we have seen features that were implemented as routines. An alternative to implementing a feature as a routine is implementing it as a field.

Argument Fields

Let us start with a small example that declares a feature Point with two arguments x and y of type i32, which is a 32-bit signed integer value:

The arguments x and y are inner features of Point. Argument features are always fields. Fields can store a value, they are similar to function features without arguments and a pre-determined result value.

The example creates two instances of Point with values Point 3 4 and Point 5 12 and passes these to routine show. show then calls Point.x and Point.y on its argument p to print the point's coordinates.

Non-Argument Inner Fields

Fields can also be declared in the code section of a routine. The following example declares two fields p1 and p2 of type Point. As part of the declaration, instances of Point with values Point 3 4 and Point 5 12 are created. These points are assigned to p1 and p2 using the assignment operator :=.

Like before, these points are passed to a routine show, but this time they are read from fields p1 and p2.

Type Inference for Fields

Similar to the result type inference of function features, a field's type can be inferred automatically from the value assigned to it, the type in the declaration of p1 and p2 in this example can be omitted:

Type Inference for Argument Fields

Fuzion can in some cases even infer the types of a feature's argument fields. That way, feature declarations will look similar to implementations in a dynamically typed language like Python. In the example, we can omit the type in the declaration of Point and show:

Repeated Field Declarations

Fields that are accessed locally only may be declared with equal names shadowing previous declarations. The following example declares four different fields n and prints their values in calls to say:

However, this shadowing will result in an error in case of an access from outside the scope of such a field: