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

Match Statement

The match statement provides a means to select the actual value from an expression of choice type.

Use in an Option

A very common use of a match is to test if an Option contains data or is just nil. Here is an example with a feature japaneseNumber that translates some numbers into corresponding text in Japanese. Since not all numbers are supported, the result is an Option that is present only for those numbers supported.

A match statement is then used to test if the translated result is present:

An alternative syntax for match is available using ? and |:

Use in a Choice Type

Here is an example of using a match in a function getRed that extracts the red part from a color given in RGB, RGBA or CMYK color encoding:

Match and Type Arguments

As long as a match can be identified without ambiguity, generic parameters can be omitted in the matched type:

When there is ambiguity, however, this does not work:

Match and destructuring

Fuzion's match expression (currently) does not destructure the value: The value of a choice type can have one of several defined types, so a match matches these types, not their structure.

One can, however, destructure the value into a tuple in the next step, as shown here:

? :, postfix ?, etc.

TBW: The ternary operator ? : is currently not supported. Since type bool is a choice type, it seems to make sense to generalize ? : to work on arbitrary values of a choice type.

Similarly, postfix ? could be used to extract the first type of a choice and otherwise return the value immediately from the current feature. Then, we could do things like

  readInt i32 | eof | error { ... }

  readPoint Point | eof | error
    Point(readInt?, readInt?)

to propagate the end-of-file or error conditions. This would be a generalization of the ? operator in Rust that, as I understand, supports enums Option and Result only.