Fuzion Logo
flang.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<Object>. This avoids run-time errors such as the infamous ArrayStoreException found in Java.

Constrained Generic Types

TBW: Generic type constraints are not supported yet.

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,A...> ref
  {
    abstract call(a A) R;
  }

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.