Fuzion Logo
fuzion-lang.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 fruit between types apple, pear or banana can hence be defined as follows:

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 apple pear banana) => ...

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:

Using the keyword of in the declaration of a feature inheriting from choice provides a convenient syntax to declare features and use them as alternatives in the choice type:

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:

option(T type) : 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 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 type...) 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.