Actual Type Parameters
Features of routine type can be generic. This means they can be defined with type parameters that need to be replaced by actual types when the feature is called.
Generic List Example
Here is the list example from section References
extended with
actual type parameters. These type parameters are then used to create a list
of i32 elements and a list of String elements.
Compatibility of Features with Actual Type Parameters
Two types that differ only in the actual type parameters are incompatible
in Fuzion. This means, e.g., that a value of
type array String cannot be assigned to a field of
type array Any. This avoids run-time errors such as the
infamous ArrayStoreException found in Java.
Constrained Type Parameters
Actual type parameters can be constrained as shown in this example:
It is possible to call any feature of the constraint:
Free Types
The kind of type parameters seen above, where the type parameter is explicitly declared as a parameter of a feature is called a bound type in Fuzion. Fuzion also supports so-called free types: you can declare a type parameter by using a so-far unused identifier in place of a type:
While bound types can be specified explicitly in feature calls to override type inference information, it is forbidden to explicitly specify a free type parameter in a function call.
Open Type Parameters
Fuzion allows the definition of open type parameters, i.e., type parameters that can be replaced by an arbitrary number of actual types. Open type parameters are seldom useful in application code, but they provide a powerful mechanism to define Choice Types , Functions and Tuples using generic Fuzion features.
As an example from the standard library, this is the code for a Function:
Function(R type, A type...) ref is call(a A) R => abstract
Function declares an abstract feature call that
returns a result of type R specified as an actual type parameter and
that receives zero or more actual arguments defined by the open type parameter
A....
Fields of an open type parameter can be accessed for instances with actual
arguments that are not open. Since there might be an arbitrary number of
actual arguments, there might be an arbitrary number of such fields.
Field accesses therefore require discriminators .0,
.1, etc. to select the field corresponding to the first, second,
etc. actual argument. See Tuples
for an example of how this
is done.