Maybe<T>
Namespace: System · struct
Implements: IEquatable<Maybe<T>>, IEquatable<T>, IComparable<Maybe<T>>, IComparable<T>, IFormattable
Represents a value that may or may not be present, along with an optional error state.
Constructors
Maybe<T>
Maybe<T>(T value)
Properties
CapturedError
ExceptionDispatchInfo CapturedError { get; }
Returns the captured error context, or null if there wasn't any
Error
Exception Error { get; }
Returns the capture Exception, or null if no error occurred.
Failed
bool Failed { get; }
The value failed to compute
Returns: !(HasValue || IsEmpty)
HasValue
bool HasValue { get; }
There is a value
!(IsEmpty || HasFailed)
IsEmpty
bool IsEmpty { get; }
No value was returned
!(HasValue || Failed)
Value
T Value { get; }
Returns the value if the computation succeeded
Methods
Bind
static Func<Maybe<T>, Maybe<TResult>> Bind<TResult>(Func<T, Maybe<TResult>> computation)
static Func<Maybe<T>, Maybe<T>, Maybe<TResult>> Bind<TResult>(Func<T, T, Maybe<TResult>> computation)
Check
bool Check(out T result, out MaybeError error)
Tests if the value failed to compute
result— Receives the result of the computation, if it completed successfullyerror— Receives the captured error if the computation has failed.
Returns: true if the computation has completed successfully; otherwise, false.
Maybe<int> ComputeInner() { .... }
Maybe<string> ComputeOuter()
{
if (!ComputeSomething().Check(out var result, out var error))
{
return error; // auto-cast to Maybe<string> with the error
}
return "inner = " + result;
}
CompareTo
int CompareTo(Maybe<T> other)
int CompareTo(T other)
Equals
bool Equals(Maybe<T> other)
bool Equals(T other)
bool Equals(object obj)
Failure
static Maybe<T> Failure(Exception error)
static Maybe<T> Failure(ExceptionDispatchInfo error)
static Maybe<T> Failure(MaybeError error)
GetHashCode
int GetHashCode()
GetValueOrDefault
T GetValueOrDefault()
Returns the value if the computation succeeded, or default(T) in all other cases
Resolve
T Resolve()
Returns the result of the computation, or re-throws any captured exception
Maybe<string> ComputeSomething() { .... }
var result = ComputeSometing().Resolve(); // throws if failed
Console.WriteLine("Result is: " + result);
Return
static Func<Maybe<T>, Maybe<TResult>> Return<TResult>(Func<T, TResult> computation)
ThrowForNonSuccess
void ThrowForNonSuccess()
Rethrows any captured error, if there was one.
ToString
string ToString()
string ToString(string format, IFormatProvider formatProvider)
Fields
Default
static readonly Maybe<T> Default
Represents a result that is equal to the default of a type (0, false, null, ...)
EmptyTask
static readonly Task<Maybe<T>> EmptyTask
Cached completed Task that always return an empty value
Nothing
static readonly Maybe<T> Nothing
Represents an empty result (no computation)