The option type is a predefined variant type that is used to express whether there is a value of some type or none. This is especially useful when calling a partial function, that is, a function that is not defined for some inputs. In that case, the value of the option type would be None, otherwise Some (v), where v is some meaningful value of any type.
An example in arithmetic is the division operation:
switch <variable> { | Some (<value_name>) => <block_code> | None => <block_code> }
Initialization
The keyword Some can be used to create an option variable for a given value. The keyword None can be used to create an option variable with no given value.
let middleName : string option = Some "Foo";let noMiddleName : string option = None;
Option in Pattern matching
In the previous chapters, you’ve seen how to do pattern matching using the switch operator. The keyword Some can be used in a pattern matching to retrieve the value behind the option variable. The keyword None can be used in a pattern matching to verify the option variable has no value.
switch <variable> { | Some (<value_name>) => <block_code> | None => <block_code> }
<block_code> can be a single instruction or a block {} <value_name> is a local variable name. <value_name> which holds the option value and can be used inside the <block_code>
Here is an example of accessing maps returning an option type and retrieving the value behind the optional :
type expected_type = int type balance_type = map(nat, expected_type) let user_balances: balance_type = Map.literal ([ (1n, 10) ]); let my_balance : option(expected_type) = Map.find_opt (1n, user_balances); let bal : expected_type = switch (my_balance) { | Some (v) => v | None => (failwith ("Unknown user") : expected_type) };
Here is an example of pattern matching resolving an option type directly (useful when we just want to retrieve the value behind the optional) :
let bal2 : expected_type = switch (Map.find_opt (1n, user_balances)) { | Some (v) => v | None => (failwith ("Unknown user") : expected_type) };
⚠️ Notice the cast of failwith instruction into an expected\type_
Your mission
1- Notice the weapons mapping which maps the name of each weapon to its corresponding input of power. We want to increase the power of the Main Laser but mapping returns optional results as they might not be found in the mapping. Define the constant main_laser_power as an optional int from selecting “Main Laser” from the weapons mapping.
2- Write a pattern matching for main_laser_power. If it exists, increase the power of the “Main Laser” by 1 (use i as temporary matching variable). If it does not exist in the mapping, fail with “Weapon not found”