Struct Maybe<T>
- Namespace
- LfrlAnvil.Functional
- Assembly
- LfrlAnvil.Functional.dll
Represents a generic optional value.
public readonly struct Maybe<T> : IEquatable<Maybe<T>>, IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable where T : notnull
Type Parameters
T
Value type.
- Implements
-
IEquatable<Maybe<T>>IEnumerable<T>
- Inherited Members
- Extension Methods
Fields
None
Represents a lack of value.
public static readonly Maybe<T> None
Field Value
- Maybe<T>
Properties
HasValue
Specifies whether or not this instance contains a non-null value.
public bool HasValue { get; }
Property Value
Methods
Bind<T2>(Func<T, Maybe<T2>>)
Returns the result of the provided some
delegate invocation when HasValue is equal to true,
otherwise returns this instance.
[Pure]
public Maybe<T2> Bind<T2>(Func<T, Maybe<T2>> some) where T2 : notnull
Parameters
Returns
Type Parameters
T2
Result type.
Bind<T2>(Func<T, Maybe<T2>>, Func<Maybe<T2>>)
Returns the result of the provided some
delegate invocation when HasValue is equal to true,
otherwise returns the result of the provided none
delegate invocation.
[Pure]
public Maybe<T2> Bind<T2>(Func<T, Maybe<T2>> some, Func<Maybe<T2>> none) where T2 : notnull
Parameters
some
Func<T, Maybe<T2>>Delegate to invoke when HasValue is equal to true.
none
Func<Maybe<T2>>Delegate to invoke when HasValue is equal to false.
Returns
Type Parameters
T2
Result type.
Equals(Maybe<T>)
Indicates whether the current object is equal to another object of the same type.
[Pure]
public bool Equals(Maybe<T> other)
Parameters
other
Maybe<T>An object to compare with this object.
Returns
Equals(object?)
Indicates whether this instance and a specified object are equal.
[Pure]
public override bool Equals(object? obj)
Parameters
obj
objectThe object to compare with the current instance.
Returns
- bool
true if
obj
and this instance are the same type and represent the same value; otherwise, false.
GetHashCode()
Returns the hash code for this instance.
[Pure]
public override int GetHashCode()
Returns
- int
A 32-bit signed integer that is the hash code for this instance.
GetValue()
Gets the underlying value.
[Pure]
public T GetValue()
Returns
- T
Underlying value.
Exceptions
- ValueAccessException
When underlying value does not exist.
GetValueOrDefault()
Gets the underlying value or a default value when it does not exist.
[Pure]
public T? GetValueOrDefault()
Returns
- T
Underlying value or a default value when it does not exist.
GetValueOrDefault(T)
Gets the underlying value or a default value when it does not exist.
[Pure]
public T GetValueOrDefault(T defaultValue)
Parameters
defaultValue
TDefault value to return in case the underlying value does not exist.
Returns
- T
Underlying value or a default value when it does not exist.
IfNone(Action)
Invokes the provided none
delegate when HasValue is equal to false,
otherwise does nothing.
public Nil IfNone(Action none)
Parameters
Returns
IfNoneOrDefault<T2>(Func<T2>)
Returns the result of the provided none
delegate invocation when HasValue is equal to false,
otherwise returns a default value.
[Pure]
public T2? IfNoneOrDefault<T2>(Func<T2> none)
Parameters
Returns
- T2
Delegate invocation result or a default value.
Type Parameters
T2
Result type.
IfNoneOrDefault<T2>(Func<T2>, T2)
Returns the result of the provided none
delegate invocation when HasValue is equal to false,
otherwise returns a default value.
[Pure]
public T2 IfNoneOrDefault<T2>(Func<T2> none, T2 defaultValue)
Parameters
none
Func<T2>Delegate to invoke when HasValue is equal to false.
defaultValue
T2Value to return HasValue is equal to true.
Returns
- T2
Delegate invocation result or a default value.
Type Parameters
T2
Result type.
IfNone<T2>(Func<T2?>)
Returns the result of the provided none
delegate invocation when HasValue
is equal to false, otherwise returns None.
[Pure]
public Maybe<T2> IfNone<T2>(Func<T2?> none) where T2 : notnull
Parameters
Returns
Type Parameters
T2
Result type.
IfSome(Action<T>)
Invokes the provided some
delegate when HasValue is equal to true, otherwise does nothing.
public Nil IfSome(Action<T> some)
Parameters
Returns
IfSomeOrDefault<T2>(Func<T, T2>)
Returns the result of the provided some
delegate invocation when HasValue is equal to true,
otherwise returns a default value.
[Pure]
public T2? IfSomeOrDefault<T2>(Func<T, T2> some)
Parameters
Returns
- T2
Delegate invocation result or a default value.
Type Parameters
T2
Result type.
IfSomeOrDefault<T2>(Func<T, T2>, T2)
Returns the result of the provided some
delegate invocation when HasValue is equal to true,
otherwise returns a default value.
[Pure]
public T2 IfSomeOrDefault<T2>(Func<T, T2> some, T2 defaultValue)
Parameters
some
Func<T, T2>Delegate to invoke when HasValue is equal to true.
defaultValue
T2Value to return HasValue is equal to false.
Returns
- T2
Delegate invocation result or a default value.
Type Parameters
T2
Result type.
IfSome<T2>(Func<T, T2?>)
Returns the result of the provided some
delegate invocation when HasValue is equal to true,
otherwise returns None.
[Pure]
public Maybe<T2> IfSome<T2>(Func<T, T2?> some) where T2 : notnull
Parameters
Returns
Type Parameters
T2
Result type.
Match(Action<T>, Action)
Invokes the provided some
delegate when HasValue is equal to true,
otherwise invokes the provided none
delegate.
public Nil Match(Action<T> some, Action none)
Parameters
some
Action<T>Delegate to invoke when HasValue is equal to true.
none
ActionDelegate to invoke when HasValue is equal to false.
Returns
Match<T2>(Func<T, T2>, Func<T2>)
Returns the result of the provided some
delegate invocation when HasValue is equal to true,
otherwise returns the result of the provided none
delegate invocation.
[Pure]
public T2 Match<T2>(Func<T, T2> some, Func<T2> none)
Parameters
some
Func<T, T2>Delegate to invoke when HasValue is equal to true.
none
Func<T2>Delegate to invoke when HasValue is equal to false.
Returns
- T2
Delegate invocation result.
Type Parameters
T2
Result type.
ToString()
Returns a string representation of this Maybe<T> instance.
[Pure]
public override string ToString()
Returns
- string
String representation.
Operators
operator ==(Maybe<T>, Maybe<T>)
Checks if a
is equal to b
.
[Pure]
public static bool operator ==(Maybe<T> a, Maybe<T> b)
Parameters
Returns
- bool
true when operands are equal, otherwise false.
explicit operator T(Maybe<T>)
Gets the underlying value.
[Pure]
public static explicit operator T(Maybe<T> value)
Parameters
value
Maybe<T>
Returns
- T
Underlying value.
Exceptions
- ValueAccessException
When underlying value does not exist.
implicit operator Maybe<T>(Nil)
[Pure]
public static implicit operator Maybe<T>(Nil none)
Parameters
none
NilValue to convert.
Returns
implicit operator Maybe<T>(T?)
Converts the provided value
to a Maybe<T> instance.
[Pure]
public static implicit operator Maybe<T>(T? value)
Parameters
value
TValue to convert.
Returns
operator !=(Maybe<T>, Maybe<T>)
Checks if a
is not equal to b
.
[Pure]
public static bool operator !=(Maybe<T> a, Maybe<T> b)
Parameters
Returns
- bool
true when operands are not equal, otherwise false.