ValueOption<T>
This a type to represent the presence or absence of a T
when it is a value type.
Operators
Implicit ValueOption<T> (T?)
Allows the client to set the T value to a ValueOption.
ValueOption<int> option = 1;
//option.IsSome = true
ValueOption<int> option = null;
//option.IsSome = false
Implicit T
Allows the client to get the T value from a ValueOption.
ValueOption<int> option = 1;
Console.Write(option);
//1
Implicit bool
Allows the client to know if a ValueOption contains value.
Option<int> option = 1;
if(option)
Console.WriteLine("Some");
else
Console.WriteLine("None");
//"Some"
Properties
IsSome
Indicates if an ValueOption is Some, holding a T.
ValueOption<int> option = 1;
Console.WriteLine(option.IsSome);
//true
IsNone
Indicates if an Option is Nome, holding no value.
Option<int> option = ValueOption<int>.None();
Console.WriteLine(option.IsNome);
//true
Methods
static Some(T)
Returns an instance of ValueOption with T
value.
var option = ValueOption.Some(10);
Console.Write(option);
//10
static None
Returns an instance of ValueOption with no Value.
var option = ValueOption.None();
Console.Write(option);
//throw a ValueOptionException
Bind<TResult> (Func<T, ValueOption<TResult>>)
Executes a given function over a ValueOption, taking its T
value, and returning an ValueOption<TResult>
.
ValueOption<int> option = 1;
ValueOption<bool> bound = option.Bind((r) => r > 0 ? true : false));
Console.Write(bound);
//true
Contains (T)
Verify if a ValueOption is Some
and it’s T
is equal to a given T
. If so returns true
otherwise returns false
.
ValueOption<int> option1 = 1;
ValueOption<int> option2 = ValueOption<int>.None();
Console.Write(option1.Contains(option2));
//false
Count
Verify if an Option is Some
. If so returns 1
otherwise returns 0
.
ValueOption<bool> option = true;
Console.Write(option.Count());
//1
DefaultValue (T)
Returns T
from a ValueOption if it is Some
, otherwise returns the given default value.
var option = ValueOption<int>.None();
Console.Write(option.DefaultValue(25));
//25
DefaultWith (Func<T>)
Returns T
from a ValueOption if it is Some
, otherwise returns the result of the given function.
ValueOption<int> option = ValueOption<int>.None();
Console.Write(option.DefaultWith(() => 25));
//25
Exists (Func<T, bool>)
Verify if T
matches a given predicate and returns true
if so or false
if not or ValueOption is None
.
ValueOption<int> option = 1;
Console.Write(option.Exists((v) => v > 0));
//true
Filter (Func<ValueOption<T>, bool>)
Return the ValueOption itself if its None
or the predicate is true
, otherwise returns None
.
ValueOption<int> option = 1;
var result = option.Filter((o) => o > 0));
//1
Flaten (ValueOption<ValueOption<T>>)
Returns T
of the inner ValueOption instance if it is Some
, otherwise None
.
ValueOption<ValueOption<bool>> ValueOption<ValueOption<bool>>.Some(true);
Console.Write(option.Flaten());
//true
Fold (Func<TState, T, TState>, TState)
Updates and returns TState
by the given function if ValueOption is Some
, otherwise returns TState
ValueOption<int> option = 1;
Console.Write(option.Fold((s, t) => s + t), 10);
//11
FoldBack (Func<T, TState, TState>, TState)
As Fold but inverting T
and TState
as the given function parameters order.
ValueOption<int> option = 1;
Console.Write(option.FoldBack((r, s) => s + r), 10);
//11
ForAll (Func<T, bool>)
Verifies if ValueOption is None
. If so returns true
, otherwise applies the given function to T
.
ValueOption<int> option = 1;
Console.Write(option.ForAll((r) => r > 10));
//false
Get
Returns the T
if Some
or throws an exception if None
.
ValueOption<int> option = ValueOption<int>.None());
Console.Write(option.Get());
//throws ValueOptionException;
Iter (Action<T>)
Executes the given action if ValueOption is Some
.
ValueOption<int> option = 1;
option.Iter(() => Console.Write("It's ok!"));
//"It's ok!"
Map (Func<T, ValueOption<T>>)
Returns the ValueOption itself if None
or the result of the given function applied to T
.
ValueOption<int> option = 1;
Console.Write(option.Map((r) => r > 0 ? true : false));
//true
Map2 (Func<ValueOption<T>>, Func<T, T, ValueOption<T>>)
Returns None
if the Option itself and the first parameter Option are None
, otherwise returns the result of the given function with the Option itself and the first parameter Option.
ValueOption<int> option1 = 1;
ValueOption<int> option2 = 2;
Console.Write(option1.Map2((o1, o2) => o1 == o2 ? o1 : 0));
//0
Match (Action<T>, Action)
Executes the first action based on T
if ValueOption is Some
or the second action if None
.
ValueOption<int> option = 1;
option.Match((t) => Console.Write(t)), () => Console.Write("None"));
//1
OrElse (Option<T>)
Returns the ValueOption itself if Some
or the first parameter.
var option = ValueOption<T>.None();
Console.Write(option.OrElse(2));
//2
OrElseWith (Func<Option<T>>)
Returns the Option itself if Some
or the result of the given function.
var option = ValueOption<T>.None();
Console.Write(option.OrElseWith(() => 25)));
//25
ToArray
Returns an array with T
if Option is Some
, otherwise an empty one.
ValueOption<int> option = 1;
Console.Write(option.ToArray().Length);
//1
ToList
As ToArray but returns a generic list with T
.
ValueOption<int> option = 1;
Console.Write(option.ToList().Count);
//1
ToValueOption
Returns Some
if T
is not null
, or a None
if so.
var myString = "Option!";
var option = myString.ToOption();
//Some ("Option!")