OptTUnwrapOr Method

Overload List

UnwrapOr(FuncT) Returns the underlying value when IsSome; or returns lazyFallbackValue() when IsNone. This is a safe way to unwrap the optional, by explicitly handling the None variant. Use the eager UnwrapOr(T) variant if the fallback value is cheap or readily available.
C#
static int GetCapacity(IEnumerable<T> collection, Opt<int> givenCapacity) {
    // capacity will be either the givenCapacity, or the number of elements in the collection.
    // note that, collection.Count() might be expensive requiring linear search.
    // lazy call avoids this call when givenCapacity.IsSome.
    return givenCapacity.UnwrapOr(() => collection.Count());
}
UnwrapOr(T) Returns the underlying value when IsSome; or returns the fallbackValue when IsNone. This is a safe way to unwrap the optional, by explicitly handling the None variant. Use the lazy UnwrapOr(FuncT) variant if the computation of the fallback value is expensive.
C#
Assert(Some(42).UnwrapOr(7) == 42);
Assert(None<int>().UnwrapOr(7) == 7);

See Also