Option<T>
This type represents an optional value.
Instances of Option<T> encapsulates the value as a container and only allow access through the Match e Match2 methods.
Option values are considered in IsNone state when the encapsulated value are equals to null or equals to its default.
Properties
| Name | Type | Description |
|---|---|---|
| IsSome | bool | Returns true when the value is not null and is not equals to its default value. Otherwise, returns false. |
| IsNone | bool | Returns true when the value is null or equals to its default value. Otherwise, returns false. |
Constructors
| Parameters | Returns | Description |
|---|---|---|
| T value | Option<T> | Initialize a new instance of Option<T> |
Methods
| Name | Parameters | Returns | Description |
|---|---|---|---|
| Some | T value | Option<T> | Creates Some<T> if the parameter is not null or equals to its default value. Otherwise creates None. |
| None | Option<T> | Creates Option<T> with IsNone state. | |
| Match | Func<T, TResult> methodWhenSome Func<TResult> methodWhenNone |
TResult | This allows a sophisticated way to apply some method for Option<T> values without having to check for the existence of a value. |
| Match2 | Option<T2> option2 Func<T, T2, TResult> methodWhenSome Func<TResult> methodWhenNone |
TResult | This allows a sophisticated way to apply some method for two different Option<T> values without having to check for the existence of a value. |
| Match | Action<T> actionWhenSome Action actionWhenNone |
void | This allows a sophisticated way to apply some method for Option<T> values without having to check for the existence of a value. |
| Match2 | Option<T2> option2 Action<T, T2> actionWhenSome Action actionWhenNone |
void | This allows a sophisticated way to apply some method for two different Option<T> values without having to check for the existence of a value. |
Operators Overload
| Operator | Parameters | Description |
|---|---|---|
| Implicit Cast |
T value |
Creates Some<T> if the parameter is not null or equals to its default value. Otherwise creates None. |
Usage
You can use an Option value in so many different ways.
Creating an option value
You can use the constructor to create option values in IsSome or IsNone states, according its parameter.
Constructors
Option<int> valueWithSome = new Option<int>(10); //-> IsSome
Option<int> valueWithNone = new Option<int>(0); //-> IsNone
You can also use the static methods Some or None to define the desired state of the value.
The Some method can be little tricky because it there's no guarantees that the created value will be at IsSome state, because it depends on the value passed as parameter.
Some and None methods
Option<int> valueWithSome = Option<int>.Some(10); //-> IsSome
Option<int> valueWithNone = Option<int>.Some(0); //-> IsNone
Option<int> value2WithNone = Option<int>.None(); //-> IsNone
Finally the simplest version: implict cast. Because of this feature you don't need to worry about any kind of syntax, you just create the value as regular types.
Implicit cast
Option<int> valueWithSome = 10; //-> IsSome
Option<int> valueWithNone = 0; //-> IsNone
Through this implicit cast you can creates new option values by using regular return commands.
See:
private Option<string> GetTextIfEven(int value)
{
if (value % 2 == 0)
return "Even";
else
return null;
}
In a more functional way:
private Option<string> GetTextIfEven(int value)
=> value % 2 == 0 ?
"Even"
: null;
Realize the fact that the method returns just a regular string or null values, without creates an Option value.
This method works fine, but is more elegant to use the None method of Option class instead of null.
private Option<string> GetTextIfEven(int value)
=> value % 2 == 0 ?
"Even"
: Option<string>.None();
Getting value from an option
To get the value encapsulated by an Option<T> method is necessary uses Match or Match2 methods. There aren't other way to get this value, so, you need to treat both cases properly.
The Match method receive two different functions as parameters, one for each case: Some and None state.
Match with named parameters
Option<int> optionalValue = 10;
int value = optionalValue.Match(
methodWhenSome: number => number,
methodWhenNone: () => 0);
The first method is executed when the value IsSome, because of it, this method receive an T value as parameter (int in this example).
The second method is executed when the value IsNone, because of it, there's no parameter in this method.
These two functions are just regular parameters, so, you can omit the its names.
Match
Option<int> optionalValue = 10;
int value = optionalValue.Match(
number => number,
() => 0);
//value = 10
Besides that, you can also apply some transformation in value before got it, like square the value:
Option<int> optionalValue = 10;
int value = optionalValue.Match(
number => number * number,
() => 0);
//value = 100
You can also returns any value in IsNone state, in the previous examples were used the zero value, but it isn't obrigatory.
Sometimes is necessary compare more than one Option values, this type provides the Match2 method to compare two different values instead of just one.
Match2
Option<int> optionalValue = 10;
Option<int> optionalValue2 = 15;
int value = optionalValue.Match2(
optionalValue2,
(number1, number2) => number1 + number2,
() => 2);
//value = 25
In the previous sample a sum of two different Option value was made. In this particular case, both Option was in IsSome state, so the first method was executed:
(number1, number2) => number1 + number2
In this anonymous method the number1 parameter received the value from optinalValue (10) and number2 from optionalValue2(15), resulting 25.
The second method is executed when any Option value is in IsNone state.
Match2
Option<int> optionalValue = 10;
Option<int> optionalValue2 = Option<int>.None();
int value = optionalValue.Match2(
optionalValue2,
(number1, number2) => number1 + number2,
() => 2);
// value = 2
There's an overload of Match and Match2 methods to receive an Action as parameter instead of a Func.
The fundamentals about Option values can be find in Fundamentals > Option Values section.