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

Choice Types

Choice types provide a way to create types that are disjoint unions of other types.

In Fuzion, choice types are provided using the standard library feature choice. With some exceptions, they can be used just like any other feature in Fuzion.

Syntax examples

choice is a generic feature in fuzion's standard library. The different alternative types of the choice are provided as generic arguments to choice. To define a concrete choice type, one can define a feature that inherits from choice giving the alternative as generic arguments. A choice between types apple, pear or banana can hence be defined as follows:

  fruit : choice<apple, pear, banana> is

A complete example using this type is this:







Alternatively, the generic type choice can also be used directly where a type is expected, e.g. defining the argument type of color in the previous example:

  color(f choice) => ...

Fuzion provides some syntax sugar for a choice type with actual generic parameters: A list of types separated by pipes (|) is treated like a choice with these types q as actual generics. So the argument type in color could as well be defined as:

  color(f apple | pear | banana>) => ...

The example from above change to use this syntax becomes:







Syntactic sugar could go further and allow the pipe syntax in the inherits part of a feature declaration, or even allow the declaration of the actual types inline as in the following example:







Generic Choice Types

Choice types might themselves be generic. An example is the feature Option from the standard library that receives a generic parameter itself:

  public Option<T> : choice<T, nil>
  {
  }

Here is an example use of an optional String:







Defining Data Types using Choice Types

Choice types can be used to implement structured data types. The following example shows the definition of a type Tree that implements a binary tree. A binary tree is either empty, which is represented by nil, or a single leaf node T, or a Branch that consists of a left and a right sub-tree:

.






Implementation Details

In the spirit of Fuzion, a choice type is nothing separate, but a feature based on the choice feature in the standard library. choice is defined in the standard library as a generic feature with an open generics list as follows:

  choice<A...> is

The Fuzion implementation ensures that instances of a choice type provide memory space to store the different alternative types. Also, the implementation might have to add a hidden tag field that is used in a match expression to distinguish the different alternatives.

In some cases, e.g. in the standard library feature bool that is a choice between unit types TRUE and FALSE, instances do not need to store any data except for the hidden tag field.

In other cases, e.g., a choice between disjoint reference types, a tag field might not be needed since the alternatives contain type information that can be used in a match clause. This is also true for an Option of a reference type, which the implementation can represent exactly as the null reference used in other languages.