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

Generics

Features of the routine type can be generic. That 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 generic type parameters. These type parameters are then used to create a list of i32 elements and a list of String elements.

Compatibility of Generic Types

Two types that differ only in the actual generic 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 Generic Types

Generic types 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:

Open Generic Types

Fuzion allows the definition of open generic types, i.e., generic types that can be replaced by an arbitrary number of actual generic types. Open Generic types 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 a generic argument and that receives zero or more arguments defined by the open generic argument A....

Fields of an open generic type can be accessed for instances with actual generic parameters that are not open. Since there might be an arbitrary number of actual generic types, 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 generic argument See Tuples for an example of how this is done.

last changed: 2024-06-28