Ferrous API Reference

All Types / Ferrous.Results

struct Option<T> [MustUse]

public readonly struct Option<T> : IEquatable<Option<T>>, IOptionValue

Represents an optional value: every Option<T> is either Some containing a value, or None containing no value. Equivalent to Rust's Option<T>.

Type Parameters
T
The type of the contained value.

Properties

public static Option<T> None

Gets an Option<T> representing the absence of a value.

public bool IsSome

Gets a value indicating whether this option contains a value.

public bool IsNone

Gets a value indicating whether this option does not contain a value.

Methods

public static Option<T> Some(T value)

Creates an Option<T> containing value.

Parameters
value
The value to wrap.

Returns: An Option<T> in the Some state.

public bool TryGetValue([MaybeNullWhen(false)] out T value)

Attempts to retrieve the contained value without throwing.

Parameters
value
When this method returns true, contains the wrapped value.

Returns: true if this option is Some; otherwise false.

public T Unwrap()

Returns the contained value, throwing InvalidOperationException if None.

public T Expect(string message)

Returns the contained value, throwing InvalidOperationException with message if None.

public T UnwrapOr(T defaultValue)

Returns the contained value or defaultValue if None.

public T UnwrapOrElse(Func<T> fallback)

Returns the contained value or computes a fallback via fallback if None.

public Option<TResult> Map<TResult>(Func<T, TResult> selector)

Maps an Option<T> to Option<TResult> by applying selector if Some.

public Option<TResult> AndThen<TResult>(Func<T, Option<TResult>> binder)

Chains another option-producing operation if Some; otherwise propagates None.

public Option<T> Filter(Func<T, bool> predicate)

Returns None if the option is None or if predicate returns false.

public TResult Match<TResult>(Func<T, TResult> onSome, Func<TResult> onNone)

Exhaustively matches both Some and None branches, returning a value.

public void Match(Action<T> onSome, Action onNone)

Exhaustively matches both Some and None branches for side effects.

public Result<T, TError> OkOr<TError>(TError error)

Transforms this Option<T> into a Result<T, TError>, mapping Some(v) to Ok(v) and None to Err(error).

public Result<T, TError> OkOrElse<TError>(Func<TError> errorFactory)

Transforms this Option<T> into a Result<T, TError>, lazily computing the error when None.

public bool Equals(Option<T> other)
public override bool Equals(object? obj)
public override int GetHashCode()
public override string ToString()