Struct Maybe<T>
- Namespace
- FunctionalDdd
- Assembly
- FunctionalDdd.RailwayOrientedProgramming.dll
Represents an optional value that may or may not exist. Similar to Nullable<T> but works with both value and reference types. Use this type to make optionality explicit in your domain model and avoid null reference exceptions.
public readonly struct Maybe<T> : IEquatable<T>, IEquatable<Maybe<T>>
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 = "John";
if (name.HasValue) Console.WriteLine(name.Value);
// Create an empty Maybe
Maybe<string> noName = Maybe.None<string>();
string result = noName.GetValueOrDefault("Default");
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(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(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(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.
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>, 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>, 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>, 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. If null, creates an empty Maybe.
Returns
- Maybe<T>
A Maybe containing the value, or an empty Maybe if value is null.
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>, 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>, 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.