Struct Option<T>
Option<T> represents an optional value: every Option<T> is either Some
and contains a value, or None
, and does not.
Inherited Members
Namespace: RustyOptions
Assembly: RustyOptions.dll
Syntax
[Serializable]
[JsonConverter(typeof(OptionJsonConverter))]
public readonly struct Option<T> : IEquatable<Option<T>>, IComparable<Option<T>>, ISpanFormattable, IFormattable where T : notnull
Type Parameters
Name | Description |
---|---|
T | The type the option might contain. |
Constructors
| Edit this page View SourceOption(T)
Creates an Option<T> containing the given value.
NOTE: Nulls are not allowed; a null value will result in a None
option.
Declaration
public Option(T value)
Parameters
Type | Name | Description |
---|---|---|
T | value | The value to wrap in an Option<T>. |
Properties
| Edit this page View SourceIsNone
Returns true
if the option is None
.
Declaration
public bool IsNone { get; }
Property Value
Type | Description |
---|---|
bool |
None
Returns the None
option for the specified T
.
Declaration
public static Option<T> None { get; }
Property Value
Type | Description |
---|---|
Option<T> |
Methods
| Edit this page View SourceAsEnumerable()
Returns an IEnumerable<T> containing either zero or one value,
depending on whether the option is None
or Some
.
Declaration
public IEnumerable<T> AsEnumerable()
Returns
Type | Description |
---|---|
IEnumerable<T> | An enumerable containing the option's value, or an empty enumerable. |
AsSpan()
Converts the option into a ReadOnlySpan<T> that contains either zero or one
items depending on whether the option is None
or Some
.
Declaration
public ReadOnlySpan<T> AsSpan()
Returns
Type | Description |
---|---|
ReadOnlySpan<T> | A span containing the option's value, or an empty span. |
CompareTo(Option<T>)
Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
Some
compares as less than any None
, while two Some
compare as their contained values would in T
.
Declaration
public int CompareTo(Option<T> other)
Parameters
Type | Name | Description |
---|---|---|
Option<T> | other |
Returns
Type | Description |
---|---|
int |
|
Equals(Option<T>)
Indicates whether the current object is equal to another object of the same type.
Declaration
public bool Equals(Option<T> other)
Parameters
Type | Name | Description |
---|---|---|
Option<T> | other | An object to compare with this object. |
Returns
Type | Description |
---|---|
bool |
|
Equals(object?)
Indicates whether the current object is equal to another object.
Declaration
public override bool Equals(object? obj)
Parameters
Type | Name | Description |
---|---|---|
object | obj | An object to compare with this object. |
Returns
Type | Description |
---|---|
bool |
|
Overrides
| Edit this page View SourceGetEnumerator()
Returns an enumerator for the option.
Declaration
public IEnumerator<T> GetEnumerator()
Returns
Type | Description |
---|---|
IEnumerator<T> | The enumerator. |
GetHashCode()
Retrieves the hash code of the object contained by the Option<T>, if any.
Declaration
public override int GetHashCode()
Returns
Type | Description |
---|---|
int | The hash code of the object returned by the IsSome(out T) method, if that
method returns |
Overrides
| Edit this page View SourceIsSome(out T)
Returns true
if the option is Some
, and returns the contained
value through value
.
Declaration
public bool IsSome(out T value)
Parameters
Type | Name | Description |
---|---|---|
T | value | The value contained in the option. |
Returns
Type | Description |
---|---|
bool |
|
Match(Action<T>, Action)
If the option is Some
, passes the contained value to the onSome
function.
Otherwise calls the onNone
function.
Declaration
public void Match(Action<T> onSome, Action onNone)
Parameters
Type | Name | Description |
---|---|---|
Action<T> | onSome | The function to call with the contained |
Action | onNone | The function to call if the option is |
Exceptions
Type | Condition |
---|---|
ArgumentNullException | Thrown if either |
Match<T2>(Func<T, T2>, Func<T2>)
Returns the result of executing the onSome
or onNone
functions, depending on the state
of the Option<T>.
Declaration
public T2 Match<T2>(Func<T, T2> onSome, Func<T2> onNone)
Parameters
Type | Name | Description |
---|---|---|
Func<T, T2> | onSome | The function to pass the value to, if the option is |
Func<T2> | onNone | The function to call if the option is |
Returns
Type | Description |
---|---|
T2 | The value returned by the called function. |
Type Parameters
Name | Description |
---|---|
T2 | The return type of the given functions. |
Exceptions
Type | Condition |
---|---|
ArgumentNullException | Thrown if either |
ToString()
Returns the text representation of the value of the current Option<T> object.
Declaration
public override string ToString()
Returns
Type | Description |
---|---|
string | The text representation of the value of the current Option<T> object. |
Overrides
| Edit this page View SourceToString(string?, IFormatProvider?)
Formats the value of the current Option<T> using the specified format.
Declaration
public string ToString(string? format, IFormatProvider? formatProvider)
Parameters
Type | Name | Description |
---|---|---|
string | format | The format to use, or a null reference to use the default format defined for the type of the contained value. |
IFormatProvider | formatProvider | The provider to use to format the value, or a null reference to obtain the format information from the current locale setting of the operating system. |
Returns
Type | Description |
---|---|
string | The value of the current instance in the specified format. |
TryFormat(Span<char>, out int, ReadOnlySpan<char>, IFormatProvider?)
Tries to format the value of the current instance into the provided span of characters.
Declaration
public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider)
Parameters
Type | Name | Description |
---|---|---|
Span<char> | destination | The span in which to write this instance's value formatted as a span of characters. |
int | charsWritten | When this method returns, contains the number of characters that were written in destination. |
ReadOnlySpan<char> | format | A span containing the characters that represent a standard or custom format string that defines the acceptable format for destination. |
IFormatProvider | provider | An optional object that supplies culture-specific formatting information for destination. |
Returns
Type | Description |
---|---|
bool |
|
Unwrap()
Returns the contained Some
value, or throws an InvalidOperationException
if the value is None
.
Declaration
public T Unwrap()
Returns
Type | Description |
---|---|
T | The value contained in the option. |
Exceptions
Type | Condition |
---|---|
InvalidOperationException | Thrown if the option does not contain a value. |
Operators
| Edit this page View Sourceoperator ==(Option<T>, Option<T>)
Determines whether one Option
is equal to another Option
.
Declaration
public static bool operator ==(Option<T> left, Option<T> right)
Parameters
Type | Name | Description |
---|---|---|
Option<T> | left | The first |
Option<T> | right | The second |
Returns
Type | Description |
---|---|
bool |
|
operator >(Option<T>, Option<T>)
Determines whether one Option
is greater than another Option
.
Declaration
public static bool operator >(Option<T> left, Option<T> right)
Parameters
Type | Name | Description |
---|---|---|
Option<T> | left | The first |
Option<T> | right | The second |
Returns
Type | Description |
---|---|
bool |
|
operator >=(Option<T>, Option<T>)
Determines whether one Option
is greater than or equal to another Option
.
Declaration
public static bool operator >=(Option<T> left, Option<T> right)
Parameters
Type | Name | Description |
---|---|---|
Option<T> | left | The first |
Option<T> | right | The second |
Returns
Type | Description |
---|---|
bool |
|
operator !=(Option<T>, Option<T>)
Determines whether one Option
is not equal to another Option
.
Declaration
public static bool operator !=(Option<T> left, Option<T> right)
Parameters
Type | Name | Description |
---|---|---|
Option<T> | left | The first |
Option<T> | right | The second |
Returns
Type | Description |
---|---|
bool |
|
operator <(Option<T>, Option<T>)
Determines whether one Option
is less than another Option
.
Declaration
public static bool operator <(Option<T> left, Option<T> right)
Parameters
Type | Name | Description |
---|---|---|
Option<T> | left | The first |
Option<T> | right | The second |
Returns
Type | Description |
---|---|
bool |
|
operator <=(Option<T>, Option<T>)
Determines whether one Option
is less than or equal to another Option
.
Declaration
public static bool operator <=(Option<T> left, Option<T> right)
Parameters
Type | Name | Description |
---|---|---|
Option<T> | left | The first |
Option<T> | right | The second |
Returns
Type | Description |
---|---|
bool |
|