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
data or is just
nil. Here is an example with a
japanese_number 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.
match statement is then used to test if the translated result
An alternative syntax for
match is available using
Use in a Choice Type
Here is an example of using a match in a
get_red 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:
Instead, all generic variants need to be treated k:
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
bool is a choice type, it seems to make sense to
? : to work on arbitrary values of a choice type.
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
read_int i32 | eof | error => ... read_point Point | eof | error => Point read_int? read_int?
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.