Struct Maybe<T>
Represents a single possibly absent value. Like Nullable
but works for reference types as well as value types.
Implements
Namespace: Pidgin
Assembly: Pidgin.dll
Syntax
public struct Maybe<T> : IEquatable<Maybe<T>>
Type Parameters
Name | Description |
---|---|
T | The type of the contained value |
Constructors
| Improve this Doc View SourceMaybe(T)
Create a Maybe<T> containing a value
Declaration
public Maybe(T value)
Parameters
Type | Name | Description |
---|---|---|
T | value | The value |
Properties
| Improve this Doc View SourceHasValue
Does the Maybe<T> contain a value?
Declaration
public bool HasValue { get; }
Property Value
Type | Description |
---|---|
Boolean | True if and only if the Maybe<T> contains a value. |
Value
Get the value from the Maybe<T>, throwing
Declaration
public T Value { get; }
Property Value
Type | Description |
---|---|
T | The value |
Methods
| Improve this Doc View SourceCast<U>()
Cast the value contained in the Maybe<T> to the specified result type
Declaration
public Maybe<U> Cast<U>()
Returns
Type | Description |
---|---|
Maybe<U> | A Maybe<T> containing this Maybe<T>'s value casted to |
Type Parameters
Name | Description |
---|---|
U | The type to cast the contained value to |
Equals(Maybe<T>)
Declaration
public bool Equals(Maybe<T> other)
Parameters
Type | Name | Description |
---|---|---|
Maybe<T> | other |
Returns
Type | Description |
---|---|
Boolean |
Equals(Nullable<Object>)
Declaration
public override bool Equals(object? other)
Parameters
Type | Name | Description |
---|---|---|
Nullable<Object> | other |
Returns
Type | Description |
---|---|
Boolean |
GetHashCode()
Declaration
public override int GetHashCode()
Returns
Type | Description |
---|---|
Int32 |
GetValueOrDefault()
Get the value from the Maybe<T>, or return a default value.
Declaration
public T GetValueOrDefault()
Returns
Type | Description |
---|---|
T | The value if HasValue is true, or a default value. |
GetValueOrDefault(T)
Get the value from the Maybe<T>, or return value
.
Declaration
public T GetValueOrDefault(T value)
Parameters
Type | Name | Description |
---|---|---|
T | value | The default value to return, if the Maybe<T> does not contain a value |
Returns
Type | Description |
---|---|
T | The value if HasValue is true, or |
GetValueOrDefault(Func<T>)
Get the value from the Maybe<T>, or return the result of calling value
.
Declaration
public T GetValueOrDefault(Func<T> value)
Parameters
Type | Name | Description |
---|---|---|
Func<T> | value | A function to call to create a default value, if the Maybe<T> does not contain a value |
Returns
Type | Description |
---|---|
T | The value if HasValue is true, or the result of calling |
Match<U>(Func<T, U>, Func<U>)
Tear down this Maybe<T> using a function for the two possible outcomes.
If HasValue is true, just
will be called. Otherwise, nothing
will be called.
Declaration
public U Match<U>(Func<T, U> just, Func<U> nothing)
Parameters
Type | Name | Description |
---|---|---|
Func<T, U> | just | Called when the result has a value |
Func<U> | nothing | Called when the result does not have a value |
Returns
Type | Description |
---|---|
U | The result of calling the |
Type Parameters
Name | Description |
---|---|
U | The return type |
OfType<U>()
Cast the value contained in the Maybe<T> to the specified result type, or return Nothing<T>() if the contained value is not an instance of U
.
Declaration
public Maybe<U> OfType<U>()
Returns
Type | Description |
---|---|
Maybe<U> | A Maybe<T> containing this Maybe<T>'s value casted to |
Type Parameters
Name | Description |
---|---|
U | The type to cast the contained value to |
Select<U>(Func<T, U>)
Project the value contained in the Maybe<T> using the specified transformation function.
Declaration
public Maybe<U> Select<U>(Func<T, U> selector)
Parameters
Type | Name | Description |
---|---|---|
Func<T, U> | selector | A transformation function to apply to the contained value |
Returns
Type | Description |
---|---|
Maybe<U> | The result of applying the transformation function to the contained value, or Nothing<T>() |
Type Parameters
Name | Description |
---|---|
U | The type of the resulting value |
SelectMany<U>(Func<T, Maybe<U>>)
Projects the element of the Maybe<T> into a possibly-absent value, and flattens the resulting value into a single Maybe<T>.
Declaration
public Maybe<U> SelectMany<U>(Func<T, Maybe<U>> selector)
Parameters
Type | Name | Description |
---|---|---|
Func<T, Maybe<U>> | selector | A transformation function to apply to the contained value |
Returns
Type | Description |
---|---|
Maybe<U> | The resulting Maybe<T>, or Nothing<T>() if the HasValue property returns false or the selector returns an absent value |
Type Parameters
Name | Description |
---|---|
U | The type of the resulting possibly-absent value |
SelectMany<U, R>(Func<T, Maybe<U>>, Func<T, U, R>)
Projects the element of the Maybe<T> into a possibly-absent value, and flattens the resulting value into a single Maybe<T>, applying a result selector function to the two values.
Declaration
public Maybe<R> SelectMany<U, R>(Func<T, Maybe<U>> selector, Func<T, U, R> result)
Parameters
Type | Name | Description |
---|---|---|
Func<T, Maybe<U>> | selector | A transformation function to apply to the contained value |
Func<T, U, R> | result | A transformation function to apply to the contained value and the value contained in the selected Maybe<T> |
Returns
Type | Description |
---|---|
Maybe<R> | The result of applying |
Type Parameters
Name | Description |
---|---|
U | The type of the value to select |
R | The type of the resulting possibly-absent value |
Where(Func<T, Boolean>)
Filter a Maybe<T> according to a predicate.
Declaration
public Maybe<T> Where(Func<T, bool> predicate)
Parameters
Type | Name | Description |
---|---|---|
Func<T, Boolean> | predicate | A predicate to apply to the value contained within the Maybe<T>. |
Returns
Type | Description |
---|---|
Maybe<T> | A Maybe<T> containing the current Maybe<T>'s Value, if the HasValue property returns true and the value satisfies the predicate, or Nothing<T>() |
Operators
| Improve this Doc View SourceEquality(Maybe<T>, Maybe<T>)
Declaration
public static bool operator ==(Maybe<T> left, Maybe<T> right)
Parameters
Type | Name | Description |
---|---|---|
Maybe<T> | left | |
Maybe<T> | right |
Returns
Type | Description |
---|---|
Boolean |
Inequality(Maybe<T>, Maybe<T>)
Declaration
public static bool operator !=(Maybe<T> left, Maybe<T> right)
Parameters
Type | Name | Description |
---|---|---|
Maybe<T> | left | |
Maybe<T> | right |
Returns
Type | Description |
---|---|
Boolean |