Provides utility type Option<T>
for optional value handling
Install-Package Dvchevskii.Optional
dotnet add package Dvchevskii.Optional
Option<T>
from T value
Option<int> option = Option.Some(42);
Assert.IsTrue(option.IsSome());
Option<T>
Option<int> option = Option.None<int>();
Assert.IsTrue(option.IsNone());
string MapOption(Option<int> option)
{
return option.MapOrElse(
value => $"I have value: {value}",
() => "I have no value =("
);
}
Assert.AreEqual(
MapOption(Option.Some(42)),
"I have value: 42"
);
Assert.AreEqual(
MapOption(Option.None<int>()),
"I have no value =("
);
int PatternMatchOption<T>(Option<T> option) =>
option switch {
ISome => 42,
INone => 69
};
bool IsOptionSome<T>(Option<T> option) => option is ISome;