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

Calls

A call to a feature executes the code of the feature and yields the result of that feature.

Simple Calls

A call typically has the form

target.called_feature arguments

where target itself is a call that determines the instance the call is executed on and arguments specify the initial values of the argument fields of the called feature.

If target is not given, target is the instance of the current feature or, in case this feature does not declare a feature named called_feature, the target is the instance of the closest outer feature that does declare a feature called_feature.

Here is a small example that defines and calls the features double, pow and last_digit on instances of my_int:

After the declaration of my_int, the example starts by calling my_int 7 and assigning the result to a new field seven. This call does not explicity specify a target. The implicit target is the current instance of call, the argument field v will be initialized to 7.

A feature that does not define an explicit result type (or uses => and type inference to infer the result type) implicitly returns its current instance as a result. So in the case of the call g my_int 7, the result is an instance of my_int with the field v set to 7.

Next, the call seven.minus1 is performed. The target of the call is the value stored in field seven. There are no arguments expected by minus1, so none are given to the call and parentheses are also not needed. The result of the call to minus1 is a new instance of my_int.

The following call six.pow 3 calls pow on the target six given the argument 3. If you look at the code of pow, you can see that it takes the current object to the given exponent using a recursive call to itself.

Finally, at the end of this example, six.factorial calls factorial on six.

For clarity, the previous example did not use syntactic sugar. It can be simplified to equivalent code as follows:

Operators

The use of any operator such as infix + to add numeric values is a call to a feature. To define an operator, the feature name must be infix, prefix, or postfix followed by the operator to define.

Here is the same example as above using prefix, infix and postfix operators instead of normal feature names:

The use of operators as feature names is purely syntactic, the code is equivalent to the version using normal names.

It is even possible to use dot-notation for calls to features whose name is an operator. E.g., the call --seven is equivalent to seven.prefix --: