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

Fields

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 instances to a routine show that 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. Using :=, they are assigned to p1 and p2, respectively.

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 deduced automatically from the value assigned to it, the type in the declaration of p1 and p2 in this example can be omitted:

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: