Struct Option<T>
- Namespace
- UniOption
- Assembly
- UniOption.dll
public readonly struct Option<T> : IOption where T : class
Type Parameters
T
- Implements
- Inherited Members
- Extension Methods
Fields
None
Gets an Option with a None value.
public static readonly Option<T> None
Field Value
- Option<T>
Properties
IsNone
True if this Option has a None value; otherwise, false.
[Pure]
public bool IsNone { get; }
Property Value
IsSome
True if this Option has a Some value; otherwise, false.
[Pure]
public bool IsSome { get; }
Property Value
Methods
Do(Action<T>)
Performs the specified action on the value of this Option if it has a Some value.
public Option<T> Do(Action<T> ifSome)
Parameters
ifSome
Action<T>The action to perform if this Option has a Some value.
Returns
- Option<T>
This Option.
Do(Action<T>, Action)
Performs the specified actions on the value of this Option based on whether it has a Some value or a None value.
public Option<T> Do(Action<T> ifSome, Action ifNone)
Parameters
ifSome
Action<T>The action to perform if this Option has a Some value.
ifNone
ActionThe action to perform if this Option has a None value.
Returns
- Option<T>
This Option.
DoAsync(Func<T, UniTask>)
Asynchronously performs the specified async action on the value of this Option if it has a Some value.
public UniTask<Option<T>> DoAsync(Func<T, UniTask> ifSome)
Parameters
ifSome
Func<T, UniTask>The async action to perform if this Option has a Some value.
Returns
- UniTask<Option<T>>
A UniTask representing the asynchronous operation.
DoAsync(Func<T, UniTask>, Func<UniTask>)
Asynchronously performs the specified async actions on the value of this Option based on whether it has a Some value or a None value.
public UniTask<Option<T>> DoAsync(Func<T, UniTask> ifSome, Func<UniTask> ifNone)
Parameters
ifSome
Func<T, UniTask>The async action to perform if this Option has a Some value.
ifNone
Func<UniTask>The async action to perform if this Option has a None value.
Returns
- UniTask<Option<T>>
A UniTask representing the asynchronous operation.
Do<TContext>(Action<T, TContext>, TContext)
Performs the specified action, with a value reference to a context, on the value of this Option based on whether it has a Some value or a None value.
public Option<T> Do<TContext>(Action<T, TContext> ifSome, TContext context)
Parameters
ifSome
Action<T, TContext>The action to perform if this Option has a Some value.
context
TContextThe context to pass to the action.
Returns
- Option<T>
This Option.
Type Parameters
TContext
Do<TContext>(Action<T, TContext>, TContext, Action)
Performs the specified action, with a value reference to a context, on the value of this Option based on whether it has a Some value or a None value.
public Option<T> Do<TContext>(Action<T, TContext> ifSome, TContext context, Action ifNone)
Parameters
ifSome
Action<T, TContext>The action to perform if this Option has a Some value.
context
TContextThe context to pass to the action.
ifNone
ActionThe action to perform if this Option has a None value.
Returns
- Option<T>
This Option.
Type Parameters
TContext
Do<TContext>(Action<T, TContext>, TContext, Action<TContext>)
Performs the specified action, with a value reference to a context, on the value of this Option based on whether it has a Some value or a None value.
public Option<T> Do<TContext>(Action<T, TContext> ifSome, TContext context, Action<TContext> ifNone)
Parameters
ifSome
Action<T, TContext>The action to perform if this Option has a Some value.
context
TContextThe context to pass to the action.
ifNone
Action<TContext>The action to perform if this Option has a None value.
Returns
- Option<T>
This Option.
Type Parameters
TContext
Do<TContext>(ActionRef<T, TContext>, ref TContext)
Performs the specified action, with a mutable reference to a context, on the value of this Option based on whether it has a Some value or a None value.
public Option<T> Do<TContext>(ActionRef<T, TContext> ifSome, ref TContext context)
Parameters
ifSome
ActionRef<T, TContext>The action to perform if this Option has a Some value.
context
TContextThe context to pass to the action.
Returns
- Option<T>
This Option.
Type Parameters
TContext
Do<TContext>(ActionRef<T, TContext>, ref TContext, ActionRef<TContext>)
Performs the specified action, with a mutable reference to a context, on the value of this Option based on whether it has a Some value or a None value.
public Option<T> Do<TContext>(ActionRef<T, TContext> ifSome, ref TContext context, ActionRef<TContext> ifNone)
Parameters
ifSome
ActionRef<T, TContext>The action to perform if this Option has a Some value.
context
TContextThe context to pass to the action.
ifNone
ActionRef<TContext>The action to perform if this Option has a None value.
Returns
- Option<T>
This Option.
Type Parameters
TContext
Equals(object?)
public override bool Equals(object? obj)
Parameters
obj
object
Returns
Equals(Option<T>)
public bool Equals(Option<T> other)
Parameters
other
Option<T>
Returns
GetHashCode()
public override int GetHashCode()
Returns
IsSomeAnd(Func<T, bool>)
Checks if this Option has a Some value and satisfies the given predicate.
public bool IsSomeAnd(Func<T, bool> predicate)
Parameters
predicate
Func<T, bool>The predicate to check.
Returns
- bool
True if this Option has a Some value and satisfies the predicate; otherwise, false.
MapValue<TResult>(Func<T, TResult>)
Applies a mapping function to the value of this Option and returns a new ValueOption with the result.
[Pure]
public ValueOption<TResult> MapValue<TResult>(Func<T, TResult> map) where TResult : struct
Parameters
map
Func<T, TResult>The mapping function.
Returns
- ValueOption<TResult>
A ValueOption with the mapped value if this Option has a Some value; otherwise, a ValueOption with a None value.
Type Parameters
TResult
The type of the result value.
Map<TResult>(Func<T, TResult>)
Applies a mapping function to the value of this Option and returns a new Option with the result.
[Pure]
public Option<TResult> Map<TResult>(Func<T, TResult> map) where TResult : class
Parameters
map
Func<T, TResult>The mapping function.
Returns
- Option<TResult>
An Option with the mapped value if this Option has a Some value; otherwise, an Option with a None value.
Type Parameters
TResult
The type of the result value.
Match<TResult>(Func<T, TResult>, Func<TResult>)
Matches the value of this Option and applies the specified functions accordingly.
public TResult Match<TResult>(Func<T, TResult> some, Func<TResult> none)
Parameters
some
Func<T, TResult>The function to apply if this Option has a Some value.
none
Func<TResult>The function to apply if this Option has a None value.
Returns
- TResult
The result of applying the specified function to the value of this Option.
Type Parameters
TResult
The type of the result.
Match<TResult, TContext>(Func<T, TContext, TResult>, TContext, Func<TResult>)
Matches the value of this Option and applies the specified functions accordingly.
public TResult Match<TResult, TContext>(Func<T, TContext, TResult> some, TContext context, Func<TResult> none)
Parameters
some
Func<T, TContext, TResult>The function to apply if this Option has a Some value.
context
TContextThe context to pass to the function.
none
Func<TResult>The function to apply if this Option has a None value.
Returns
- TResult
The result of applying the specified function to the value of this Option.
Type Parameters
TResult
The type of the result.
TContext
The type of the context.
Match<TResult, TContext>(Func<T, TContext, TResult>, TContext, Func<TContext, TResult>)
Matches the value of this Option and applies the specified functions accordingly.
public TResult Match<TResult, TContext>(Func<T, TContext, TResult> some, TContext context, Func<TContext, TResult> none)
Parameters
some
Func<T, TContext, TResult>The function to apply if this Option has a Some value.
context
TContextThe context to pass to the function.
none
Func<TContext, TResult>The function to apply if this Option has a None value.
Returns
- TResult
The result of applying the specified function to the value of this Option.
Type Parameters
TResult
The type of the result.
TContext
The type of the context.
OfType<TValue>()
Filters the value of this Option to the specified type.
[Pure]
public Option<TValue> OfType<TValue>() where TValue : class
Returns
- Option<TValue>
An Option with the value cast to the specified type if it has a Some value and satisfies the type constraint; otherwise, an Option with a None value.
Type Parameters
TValue
The type to filter the value.
Or(T)
Returns this Option if it has a Some value; otherwise, returns the specified alternative option.
[Pure]
public Option<T> Or(T orOption)
Parameters
orOption
TThe alternative option to return if this Option has a None value.
Returns
- Option<T>
This Option if it has a Some value; otherwise, the specified alternative option.
Reduce(Func<T>)
Returns the value of this Option if it has a Some value; otherwise, returns the value returned by the specified function. This method is unsafe as it may throw an exception if the specified function throws an exception. It is recommended to use Match<TResult>(Func<T, TResult>, Func<TResult>) instead.
public T Reduce(Func<T> defaultValue)
Parameters
defaultValue
Func<T>The function that returns the default value to return.
Returns
- T
The value of this Option if it has a Some value; otherwise, the value returned by the specified function.
Reduce(T)
Returns the value of this Option if it has a Some value; otherwise, returns the specified default value.
[Pure]
public T Reduce(T defaultValue)
Parameters
defaultValue
TThe default value to return.
Returns
- T
The value of this Option if it has a Some value; otherwise, the specified default value.
Some(T)
Creates a new Option with a Some value.
public static Option<T> Some(T content)
Parameters
content
TThe value to wrap.
Returns
- Option<T>
An Option with the specified value.
ToEnumerable()
Converts this Option to an enumerable containing the value if it has a Some value; otherwise, returns an empty enumerable.
public IEnumerable<T> ToEnumerable()
Returns
- IEnumerable<T>
An enumerable containing the value of this Option if it has a Some value; otherwise, an empty enumerable.
ToString()
[Pure]
public override string ToString()
Returns
Where(Func<T, bool>)
Filters the value of this Option based on the given predicate. This method is unsafe as it may throw an exception if the specified function throws an exception. It is recommended to use Match<TResult>(Func<T, TResult>, Func<TResult>) instead.
public Option<T> Where(Func<T, bool> predicate)
Parameters
predicate
Func<T, bool>The predicate to filter the value.
Returns
- Option<T>
This Option if it has a None value or the value satisfies the predicate; otherwise, an Option with a None value.
WhereNot(Func<T, bool>)
Filters the value of this Option based on the given predicate.
public Option<T> WhereNot(Func<T, bool> predicate)
Parameters
predicate
Func<T, bool>The predicate to filter the value.
Returns
- Option<T>
This Option if it has a None value or the value does not satisfy the predicate; otherwise, an Option with a None value.
Zip<T2>(Option<T2>)
Combines the value of this Option with the value of the specified Option into a ValueOption.
[Pure]
public ValueOption<(T, T2)> Zip<T2>(Option<T2> other) where T2 : class
Parameters
other
Option<T2>The other Option to zip.
Returns
- ValueOption<(T, T2)>
A ValueOption with a tuple of the values if both Options have Some values; otherwise, a ValueOption with a None value.
Type Parameters
T2
The type of the value in the other Option.
Zip<T2>(T2)
Combines the value of this Option with the specified value into a ValueOption.
[Pure]
public ValueOption<(T, T2)> Zip<T2>(T2 other) where T2 : struct
Parameters
other
T2The specified value to zip.
Returns
- ValueOption<(T, T2)>
A ValueOption with a tuple of the values if this Option has a Some value; otherwise, a ValueOption with a None value.
Type Parameters
T2
The type of the specified value.
Operators
operator ==(Option<T>, Option<T>)
public static bool operator ==(Option<T> a, Option<T> b)
Parameters
Returns
implicit operator Option<T>(SerializableOption<T>)
[Pure]
public static implicit operator Option<T>(SerializableOption<T> serializableOption)
Parameters
serializableOption
SerializableOption<T>
Returns
- Option<T>
implicit operator Option<T>(T)
[Pure]
public static implicit operator Option<T>(T content)
Parameters
content
T
Returns
- Option<T>
operator !=(Option<T>, Option<T>)
public static bool operator !=(Option<T> a, Option<T> b)