Skip to content

OnixLabs.Core.Some

Matthew Layton edited this page Nov 15, 2026 · 2 revisions

Some<T> Class

Represents a present optional value — the case of Optional<T> that holds an underlying value.

Namespace OnixLabs.Core
Assembly OnixLabs.Core.dll (NuGet)
Inheritance object → Optional<T> → Some<T>
Implements IValueEquatable<Optional<T>> (inherited from Optional<T>)

Definition

public sealed class Some<T> : Optional<T>
    where T : notnull

Type Parameters

Parameter Description
T The type of the underlying optional value. Constrained to notnull.

Remarks

Some<T> is one of the two cases of Optional<T> and signifies that a value is present. Because Optional<T> is a closed class, a C# 15 switch over Some<T> and None<T> is exhaustive without a discard arm. Instances are created through:

The constructor is internal, so Some<T> cannot be instantiated directly. The wrapped value is exposed through the Value property.

Some<T> inherits all members of Optional<T>, including HasValue (always true for a Some<T>), the transformation methods Match, Select, SelectMany, and the value-access methods GetValueOrDefault, GetValueOrThrow.

Examples

using OnixLabs.Core;

// Create via the static factory method.
Some<int> some = Optional<int>.Some(42);
Console.WriteLine(some.Value);       // 42
Console.WriteLine(some.HasValue);    // True

// Value-type defaults are preserved as Some.
Optional<int> zero = Optional<int>.Some(0);
Console.WriteLine(zero.HasValue);    // True
Console.WriteLine(((Some<int>)zero).Value); // 0

// Pattern match to access the value.
Optional<string> optional = "hello";
if (optional is Some<string> s)
{
    Console.WriteLine(s.Value);      // hello
}

Properties

Property Type Description
Value T Gets the underlying optional value held by this instance.

Value

public T Value { get; }

Gets the underlying value held by the current Some<T> instance. This value is guaranteed to be non-null.

Returns — The underlying optional value.

See Also

ONIXLabs .NET Library


Clone this wiki locally