Struct NumericOption<T>
NumericOption<T> represents an optional number: every NumericOption<T> is either Some
and contains a number, or None
, and does not.
Implements
Inherited Members
Namespace: RustyOptions
Assembly: RustyOptions.dll
Syntax
[Serializable]
[JsonConverter(typeof(NumericOptionJsonConverter))]
public readonly struct NumericOption<T> : INumber<NumericOption<T>>, IComparable, IComparable<NumericOption<T>>, IComparisonOperators<NumericOption<T>, NumericOption<T>, bool>, IModulusOperators<NumericOption<T>, NumericOption<T>, NumericOption<T>>, INumberBase<NumericOption<T>>, IEquatable<NumericOption<T>>, ISpanParsable<NumericOption<T>>, IParsable<NumericOption<T>>, IAdditionOperators<NumericOption<T>, NumericOption<T>, NumericOption<T>>, IAdditiveIdentity<NumericOption<T>, NumericOption<T>>, IDecrementOperators<NumericOption<T>>, IDivisionOperators<NumericOption<T>, NumericOption<T>, NumericOption<T>>, IEqualityOperators<NumericOption<T>, NumericOption<T>, bool>, IIncrementOperators<NumericOption<T>>, IMultiplicativeIdentity<NumericOption<T>, NumericOption<T>>, IMultiplyOperators<NumericOption<T>, NumericOption<T>, NumericOption<T>>, ISubtractionOperators<NumericOption<T>, NumericOption<T>, NumericOption<T>>, IUnaryNegationOperators<NumericOption<T>, NumericOption<T>>, IUnaryPlusOperators<NumericOption<T>, NumericOption<T>>, ISpanFormattable, IFormattable where T : struct, INumber<T>
Type Parameters
Name | Description |
---|---|
T | The type the option might contain. |
Constructors
| Edit this page View SourceNumericOption(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 NumericOption(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 NumericOption<T> None { get; }
Property Value
Type | Description |
---|---|
NumericOption<T> |
One
Gets the value 1
for the type.
Declaration
public static NumericOption<T> One { get; }
Property Value
Type | Description |
---|---|
NumericOption<T> |
Zero
Gets the value 0
for the type.
Declaration
public static NumericOption<T> Zero { get; }
Property Value
Type | Description |
---|---|
NumericOption<T> |
Methods
| Edit this page View SourceAbs(NumericOption<T>)
Computes the absolute of a value.
Declaration
public static NumericOption<T> Abs(NumericOption<T> value)
Parameters
Type | Name | Description |
---|---|---|
NumericOption<T> | value | The value for which to get its absolute. |
Returns
Type | Description |
---|---|
NumericOption<T> | The absolute of |
Exceptions
Type | Condition |
---|---|
OverflowException | The absolute of |
AsEnumerable()
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. |
Clamp(NumericOption<T>, NumericOption<T>, NumericOption<T>)
Clamps a value to an inclusive minimum and maximum value.
Declaration
public static NumericOption<T> Clamp(NumericOption<T> value, NumericOption<T> min, NumericOption<T> max)
Parameters
Type | Name | Description |
---|---|---|
NumericOption<T> | value | The value to clamp. |
NumericOption<T> | min | The inclusive minimum to which |
NumericOption<T> | max | The inclusive maximum to which |
Returns
Type | Description |
---|---|
NumericOption<T> | The result of clamping |
Exceptions
Type | Condition |
---|---|
ArgumentException |
|
CompareTo(NumericOption<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(NumericOption<T> other)
Parameters
Type | Name | Description |
---|---|---|
NumericOption<T> | other |
Returns
Type | Description |
---|---|
int |
|
CompareTo(object?)
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.
Declaration
public int CompareTo(object? obj)
Parameters
Type | Name | Description |
---|---|---|
object | obj | An object to compare with this instance. |
Returns
Type | Description | ||||||||
---|---|---|---|---|---|---|---|---|---|
int | A value that indicates the relative order of the objects being compared. The return value has these meanings:
|
Exceptions
Type | Condition |
---|---|
ArgumentException |
|
CreateChecked<TOther>(TOther)
Creates an instance of the current type from a value, throwing an overflow exception for any values that fall outside the representable range of the current type.
Declaration
public static NumericOption<T> CreateChecked<TOther>(TOther value) where TOther : INumberBase<TOther>
Parameters
Type | Name | Description |
---|---|---|
TOther | value | The value that's used to create the instance of NumericOption<T>. |
Returns
Type | Description |
---|---|
NumericOption<T> | An instance of NumericOption<T> created from |
Type Parameters
Name | Description |
---|---|
TOther | The type of |
Exceptions
Type | Condition |
---|---|
NotSupportedException | TOther is not supported. |
OverflowException |
|
CreateSaturating<TOther>(TOther)
Creates an instance of the current type from a value, saturating any values that fall outside the representable range of the current type.
Declaration
public static NumericOption<T> CreateSaturating<TOther>(TOther value) where TOther : INumberBase<TOther>
Parameters
Type | Name | Description |
---|---|---|
TOther | value | The value which is used to create the instance of NumericOption<T>. |
Returns
Type | Description |
---|---|
NumericOption<T> | An instance of NumericOption<T> created from |
Type Parameters
Name | Description |
---|---|
TOther | The type of |
Exceptions
Type | Condition |
---|---|
NotSupportedException | TOther is not supported. |
CreateTruncating<TOther>(TOther)
Creates an instance of the current type from a value, truncating any values that fall outside the representable range of the current type.
Declaration
public static NumericOption<T> CreateTruncating<TOther>(TOther value) where TOther : INumberBase<TOther>
Parameters
Type | Name | Description |
---|---|---|
TOther | value | The value which is used to create the instance of NumericOption<T>. |
Returns
Type | Description |
---|---|
NumericOption<T> | An instance of NumericOption<T> created from |
Type Parameters
Name | Description |
---|---|
TOther | The type of |
Exceptions
Type | Condition |
---|---|
NotSupportedException | TOther is not supported. |
Equals(NumericOption<T>)
Indicates whether the current object is equal to another object of the same type.
Declaration
public bool Equals(NumericOption<T> other)
Parameters
Type | Name | Description |
---|---|---|
NumericOption<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 SourceIsEvenInteger(NumericOption<T>)
Determines if a value represents an even integral number.
Declaration
public static bool IsEvenInteger(NumericOption<T> value)
Parameters
Type | Name | Description |
---|---|---|
NumericOption<T> | value | The value to be checked. |
Returns
Type | Description |
---|---|
bool |
IsInfinity(NumericOption<T>)
Determines if a value is infinite.
Declaration
public static bool IsInfinity(NumericOption<T> value)
Parameters
Type | Name | Description |
---|---|---|
NumericOption<T> | value | The value to be checked. |
Returns
Type | Description |
---|---|
bool |
IsNaN(NumericOption<T>)
Determines if a value is NaN.
Declaration
public static bool IsNaN(NumericOption<T> value)
Parameters
Type | Name | Description |
---|---|---|
NumericOption<T> | value | The value to be checked. |
Returns
Type | Description |
---|---|
bool |
IsNegative(NumericOption<T>)
Determines if a value is negative.
Declaration
public static bool IsNegative(NumericOption<T> value)
Parameters
Type | Name | Description |
---|---|---|
NumericOption<T> | value | The value to be checked. |
Returns
Type | Description |
---|---|
bool |
IsNegativeInfinity(NumericOption<T>)
Determines if a value is negative infinity.
Declaration
public static bool IsNegativeInfinity(NumericOption<T> value)
Parameters
Type | Name | Description |
---|---|---|
NumericOption<T> | value | The value to be checked. |
Returns
Type | Description |
---|---|
bool |
IsOddInteger(NumericOption<T>)
Determines if a value represents an odd integral number.
Declaration
public static bool IsOddInteger(NumericOption<T> value)
Parameters
Type | Name | Description |
---|---|---|
NumericOption<T> | value | The value to be checked. |
Returns
Type | Description |
---|---|
bool |
IsPositive(NumericOption<T>)
Determines if a value is positive.
Declaration
public static bool IsPositive(NumericOption<T> value)
Parameters
Type | Name | Description |
---|---|---|
NumericOption<T> | value | The value to be checked. |
Returns
Type | Description |
---|---|
bool |
IsPositiveInfinity(NumericOption<T>)
Determines if a value is positive infinity.
Declaration
public static bool IsPositiveInfinity(NumericOption<T> value)
Parameters
Type | Name | Description |
---|---|---|
NumericOption<T> | value | The value to be checked. |
Returns
Type | Description |
---|---|
bool |
IsSome(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 |
|
IsZero(NumericOption<T>)
Determines if a value is zero.
Declaration
public static bool IsZero(NumericOption<T> value)
Parameters
Type | Name | Description |
---|---|---|
NumericOption<T> | value | The value to be checked. |
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 result is |
Func<T2> | onNone | The function to pass the error value to, if the result 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 |
Max(NumericOption<T>, NumericOption<T>)
Compares two values to compute which is greater.
Declaration
public static NumericOption<T> Max(NumericOption<T> x, NumericOption<T> y)
Parameters
Type | Name | Description |
---|---|---|
NumericOption<T> | x | The value to compare with |
NumericOption<T> | y | The value to compare with |
Returns
Type | Description |
---|---|
NumericOption<T> |
|
Min(NumericOption<T>, NumericOption<T>)
Compares two values to compute which is lesser.
Declaration
public static NumericOption<T> Min(NumericOption<T> x, NumericOption<T> y)
Parameters
Type | Name | Description |
---|---|---|
NumericOption<T> | x | The value to compare with |
NumericOption<T> | y | The value to compare with |
Returns
Type | Description |
---|---|
NumericOption<T> |
|
Parse(ReadOnlySpan<char>, NumberStyles, IFormatProvider?)
Parses a span of characters into a value.
Declaration
public static NumericOption<T> Parse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider? provider)
Parameters
Type | Name | Description |
---|---|---|
ReadOnlySpan<char> | s | The span of characters to parse. |
NumberStyles | style | A bitwise combination of number styles that can be present in |
IFormatProvider | provider | An object that provides culture-specific formatting information about |
Returns
Type | Description |
---|---|
NumericOption<T> | The result of parsing |
Exceptions
Type | Condition |
---|---|
ArgumentException |
|
FormatException |
|
OverflowException |
|
Parse(ReadOnlySpan<char>, IFormatProvider?)
Parses a span of characters into a value.
Declaration
public static NumericOption<T> Parse(ReadOnlySpan<char> s, IFormatProvider? provider)
Parameters
Type | Name | Description |
---|---|---|
ReadOnlySpan<char> | s | The span of characters to parse. |
IFormatProvider | provider | An object that provides culture-specific formatting information about |
Returns
Type | Description |
---|---|
NumericOption<T> | The result of parsing |
Exceptions
Type | Condition |
---|---|
FormatException |
|
OverflowException |
|
Parse(string, NumberStyles, IFormatProvider?)
Parses a string into a value.
Declaration
public static NumericOption<T> Parse(string s, NumberStyles style, IFormatProvider? provider)
Parameters
Type | Name | Description |
---|---|---|
string | s | The string to parse. |
NumberStyles | style | A bitwise combination of number styles that can be present in |
IFormatProvider | provider | An object that provides culture-specific formatting information about |
Returns
Type | Description |
---|---|
NumericOption<T> | The result of parsing |
Exceptions
Type | Condition |
---|---|
ArgumentException |
|
ArgumentNullException |
|
FormatException |
|
OverflowException |
|
Parse(string, IFormatProvider?)
Parses a string into a value.
Declaration
public static NumericOption<T> Parse(string s, IFormatProvider? provider)
Parameters
Type | Name | Description |
---|---|---|
string | s | The string to parse. |
IFormatProvider | provider | An object that provides culture-specific formatting information about |
Returns
Type | Description |
---|---|
NumericOption<T> | The result of parsing |
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
FormatException |
|
OverflowException |
|
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 +(NumericOption<T>, NumericOption<T>)
Adds two values together to compute their sum.
Declaration
public static NumericOption<T> operator +(NumericOption<T> left, NumericOption<T> right)
Parameters
Type | Name | Description |
---|---|---|
NumericOption<T> | left | The value to which |
NumericOption<T> | right | The value which is added to |
Returns
Type | Description |
---|---|
NumericOption<T> | The sum of |
operator --(NumericOption<T>)
Decrements a value.
Declaration
public static NumericOption<T> operator --(NumericOption<T> value)
Parameters
Type | Name | Description |
---|---|---|
NumericOption<T> | value | The value to decrement. |
Returns
Type | Description |
---|---|
NumericOption<T> | The result of decrementing |
operator /(NumericOption<T>, NumericOption<T>)
Divides one value by another to compute their quotient.
Declaration
public static NumericOption<T> operator /(NumericOption<T> left, NumericOption<T> right)
Parameters
Type | Name | Description |
---|---|---|
NumericOption<T> | left | The value which |
NumericOption<T> | right | The value which divides |
Returns
Type | Description |
---|---|
NumericOption<T> | The quotient of |
operator ==(NumericOption<T>, NumericOption<T>)
Determines whether one Option
is equal to another Option
.
Declaration
public static bool operator ==(NumericOption<T> left, NumericOption<T> right)
Parameters
Type | Name | Description |
---|---|---|
NumericOption<T> | left | The first |
NumericOption<T> | right | The second |
Returns
Type | Description |
---|---|
bool |
|
operator >(NumericOption<T>, NumericOption<T>)
Determines whether one Option
is greater than another Option
.
Declaration
public static bool operator >(NumericOption<T> left, NumericOption<T> right)
Parameters
Type | Name | Description |
---|---|---|
NumericOption<T> | left | The first |
NumericOption<T> | right | The second |
Returns
Type | Description |
---|---|
bool |
|
operator >=(NumericOption<T>, NumericOption<T>)
Determines whether one Option
is greater than or equal to another Option
.
Declaration
public static bool operator >=(NumericOption<T> left, NumericOption<T> right)
Parameters
Type | Name | Description |
---|---|---|
NumericOption<T> | left | The first |
NumericOption<T> | right | The second |
Returns
Type | Description |
---|---|
bool |
|
implicit operator Option<T>(NumericOption<T>)
Converts a NumericOption<T> into an Option<T>.
Declaration
public static implicit operator Option<T>(NumericOption<T> self)
Parameters
Type | Name | Description |
---|---|---|
NumericOption<T> | self | The numeric option to convert. |
Returns
Type | Description |
---|---|
Option<T> |
implicit operator NumericOption<T>(Option<T>)
Implicitly converts Option<T> into NumericOption<T>, provided that T
is compatible with NumericOption.
Declaration
public static implicit operator NumericOption<T>(Option<T> option)
Parameters
Type | Name | Description |
---|---|---|
Option<T> | option | The option to convert. |
Returns
Type | Description |
---|---|
NumericOption<T> |
implicit operator NumericOption<T>(T)
Implicitly converts a T
into a NumericOption<T>.
Declaration
public static implicit operator NumericOption<T>(T value)
Parameters
Type | Name | Description |
---|---|---|
T | value | The value to wrap in a NumericOption<T>. |
Returns
Type | Description |
---|---|
NumericOption<T> |
operator ++(NumericOption<T>)
Increments a value.
Declaration
public static NumericOption<T> operator ++(NumericOption<T> value)
Parameters
Type | Name | Description |
---|---|---|
NumericOption<T> | value | The value to increment. |
Returns
Type | Description |
---|---|
NumericOption<T> | The result of incrementing |
operator !=(NumericOption<T>, NumericOption<T>)
Determines whether one Option
is not equal to another Option
.
Declaration
public static bool operator !=(NumericOption<T> left, NumericOption<T> right)
Parameters
Type | Name | Description |
---|---|---|
NumericOption<T> | left | The first |
NumericOption<T> | right | The second |
Returns
Type | Description |
---|---|
bool |
|
operator <(NumericOption<T>, NumericOption<T>)
Determines whether one Option
is less than another Option
.
Declaration
public static bool operator <(NumericOption<T> left, NumericOption<T> right)
Parameters
Type | Name | Description |
---|---|---|
NumericOption<T> | left | The first |
NumericOption<T> | right | The second |
Returns
Type | Description |
---|---|
bool |
|
operator <=(NumericOption<T>, NumericOption<T>)
Determines whether one Option
is less than or equal to another Option
.
Declaration
public static bool operator <=(NumericOption<T> left, NumericOption<T> right)
Parameters
Type | Name | Description |
---|---|---|
NumericOption<T> | left | The first |
NumericOption<T> | right | The second |
Returns
Type | Description |
---|---|
bool |
|
operator %(NumericOption<T>, NumericOption<T>)
Divides two values together to compute their modulus or remainder.
Declaration
public static NumericOption<T> operator %(NumericOption<T> left, NumericOption<T> right)
Parameters
Type | Name | Description |
---|---|---|
NumericOption<T> | left | The value which |
NumericOption<T> | right | The value which divides |
Returns
Type | Description |
---|---|
NumericOption<T> | The modulus or remainder of |
operator *(NumericOption<T>, NumericOption<T>)
Multiplies two values together to compute their product.
Declaration
public static NumericOption<T> operator *(NumericOption<T> left, NumericOption<T> right)
Parameters
Type | Name | Description |
---|---|---|
NumericOption<T> | left | The value which |
NumericOption<T> | right | The value which multiplies |
Returns
Type | Description |
---|---|
NumericOption<T> | The product of |
operator -(NumericOption<T>, NumericOption<T>)
Subtracts two values to compute their difference.
Declaration
public static NumericOption<T> operator -(NumericOption<T> left, NumericOption<T> right)
Parameters
Type | Name | Description |
---|---|---|
NumericOption<T> | left | The value from which |
NumericOption<T> | right | The value which is subtracted from |
Returns
Type | Description |
---|---|
NumericOption<T> | The value of |
operator -(NumericOption<T>)
Computes the unary negation of a value.
Declaration
public static NumericOption<T> operator -(NumericOption<T> value)
Parameters
Type | Name | Description |
---|---|---|
NumericOption<T> | value | The value for which to compute the unary negation. |
Returns
Type | Description |
---|---|
NumericOption<T> | The unary negation of |
operator +(NumericOption<T>)
Computes the unary plus of a value.
Declaration
public static NumericOption<T> operator +(NumericOption<T> value)
Parameters
Type | Name | Description |
---|---|---|
NumericOption<T> | value | The value for which to compute the unary plus. |
Returns
Type | Description |
---|---|
NumericOption<T> | The unary plus of |