Struct Maybe<T>
- Namespace
- Trellis
- Assembly
- Trellis.Results.dll
Represents domain-level optionality — a value that was either provided or intentionally omitted.
Unlike Nullable<T> (value types only) or T? (annotation only for reference types),
Maybe<T> is a real generic type that works uniformly with both value and reference types
and composes with Result<TValue> pipelines.
public readonly struct Maybe<T> : IEquatable<T>, IEquatable<Maybe<T>> where T : notnull
Type Parameters
TThe type of the optional value. Must be a non-null type.
- Implements
-
IEquatable<T>IEquatable<Maybe<T>>
- Inherited Members
- Extension Methods
Examples
// Create a Maybe with a value
Maybe<string> name = Maybe.From("John");
if (name.HasValue) Console.WriteLine(name.Value);
// Create an empty Maybe
Maybe<string> noName = Maybe.None<string>();
string result = noName.GetValueOrDefault("Default");
// Transform optional values
Maybe<string> upper = name.Map(v => v.ToUpper());
// Consume with pattern matching
string display = name.Match(v => $"Hello, {v}!", () => "Hello, stranger!");
Properties
HasNoValue
Gets a value indicating whether this instance contains no value.
public bool HasNoValue { get; }
Property Value
- bool
True if no value is present; otherwise false.
Remarks
This is the logical inverse of HasValue. Use whichever makes your code more readable.
HasValue
Gets a value indicating whether this instance contains a value.
public bool HasValue { get; }
Property Value
- bool
True if a value is present; otherwise false.
Value
Gets the underlying value if present; otherwise throws an exception.
public T Value { get; }
Property Value
- T
The underlying value of type
T.
Remarks
Always check HasValue before accessing this property, or use GetValueOrDefault(T) instead.
Exceptions
- InvalidOperationException
Thrown when HasValue is false.
Methods
Equals(object?)
Indicates whether this instance and a specified object are equal.
public override bool Equals(object? obj)
Parameters
objobjectThe object to compare with the current instance.
Returns
- bool
true if
objand this instance are the same type and represent the same value; otherwise, false.
Equals(Maybe<T>)
Indicates whether the current object is equal to another object of the same type.
public bool Equals(Maybe<T> other)
Parameters
otherMaybe<T>An object to compare with this object.
Returns
Equals(T?)
Indicates whether the current object is equal to another object of the same type.
public bool Equals(T? other)
Parameters
otherTAn object to compare with this object.
Returns
GetHashCode()
Returns the hash code for this instance.
public override int GetHashCode()
Returns
- int
A 32-bit signed integer that is the hash code for this instance.
GetValueOrDefault(T)
Gets the underlying value if present, otherwise returns the specified default value.
public T GetValueOrDefault(T defaultValue)
Parameters
defaultValueTThe value to return when HasValue is false.
Returns
- T
The underlying value or
defaultValue.
Remarks
This is the recommended way to safely extract values from Maybe.
GetValueOrThrow(string?)
Gets the underlying value if present, otherwise throws an exception.
public T GetValueOrThrow(string? errorMessage = null)
Parameters
errorMessagestringOptional custom error message to use when throwing.
Returns
- T
The underlying value of type
T.
Remarks
Prefer GetValueOrDefault(T) or TryGetValue(out T) to avoid exceptions.
Exceptions
- InvalidOperationException
Thrown when HasValue is false.
Map<TResult>(Func<T, TResult>)
Transforms the value inside a Maybe<T> using the specified function. If no value is present, returns None<T>().
public Maybe<TResult> Map<TResult>(Func<T, TResult> selector) where TResult : notnull
Parameters
selectorFunc<T, TResult>The function to apply to the value.
Returns
- Maybe<TResult>
A Maybe containing the transformed value, or None if this instance has no value.
Type Parameters
TResultThe type of the transformed value.
Match<TResult>(Func<T, TResult>, Func<TResult>)
Pattern matches on the Maybe, calling some if a value is present
or none if no value is present.
public TResult Match<TResult>(Func<T, TResult> some, Func<TResult> none)
Parameters
someFunc<T, TResult>The function to call when a value is present.
noneFunc<TResult>The function to call when no value is present.
Returns
- TResult
The result of the matched function.
Type Parameters
TResultThe return type of the match.
ToString()
Returns the fully qualified type name of this instance.
public override string ToString()
Returns
- string
The fully qualified type name.
TryGetValue(out T)
Attempts to get the underlying value without throwing an exception.
public bool TryGetValue(out T value)
Parameters
valueTWhen this method returns true, contains the underlying value; otherwise, the default value for type
T.
Returns
- bool
True if a value is present; otherwise false.
Remarks
Similar to the TryParse pattern in .NET, this provides a safe way to check for and retrieve values.
Operators
operator ==(Maybe<T>, object?)
Determines whether a Maybe<T> equals another object.
public static bool operator ==(Maybe<T> maybe, object? other)
Parameters
Returns
- bool
True if equal; otherwise false.
operator ==(Maybe<T>, Maybe<T>)
Determines whether two Maybe<T> instances are equal.
public static bool operator ==(Maybe<T> first, Maybe<T> second)
Parameters
Returns
- bool
True if both have no value, or both have equal values; otherwise false.
operator ==(Maybe<T>, T)
Determines whether a Maybe<T> equals a value of type T.
public static bool operator ==(Maybe<T> maybe, T value)
Parameters
maybeMaybe<T>The Maybe instance to compare.
valueTThe value to compare against.
Returns
- bool
True if the Maybe has a value equal to
value; otherwise false.
implicit operator Maybe<T>(T)
Implicitly converts a value of type T to a Maybe<T>.
public static implicit operator Maybe<T>(T value)
Parameters
valueTThe value to wrap.
Returns
- Maybe<T>
A Maybe containing the value.
operator !=(Maybe<T>, object?)
Determines whether a Maybe<T> does not equal another object.
public static bool operator !=(Maybe<T> maybe, object? other)
Parameters
Returns
- bool
True if not equal; otherwise false.
operator !=(Maybe<T>, Maybe<T>)
Determines whether two Maybe<T> instances are not equal.
public static bool operator !=(Maybe<T> first, Maybe<T> second)
Parameters
Returns
- bool
True if the instances are not equal; otherwise false.
operator !=(Maybe<T>, T)
Determines whether a Maybe<T> does not equal a value of type T.
public static bool operator !=(Maybe<T> maybe, T value)
Parameters
maybeMaybe<T>The Maybe instance to compare.
valueTThe value to compare against.
Returns
- bool
True if the Maybe does not have a value equal to
value; otherwise false.