namespace Lumina.Domain.Common.Primitives;
///
/// Primitive struct for explicitly handling cases when a value might be unknown or not applicable,
/// distinguishing between "not set" and "intentionally empty".
///
/// The type of the value represented by this structure.
public readonly struct Optional
{
#region ================================================================== FIELD MEMBERS ================================================================================
private readonly T? _value;
#endregion
#region ==================================================================== PROPERTIES =================================================================================
public bool HasValue { get; }
public T Value
{
get
{
return HasValue ? _value! : throw new InvalidOperationException("Optional has no value.");
}
}
#endregion
#region ====================================================================== CTOR =====================================================================================
///
/// Overload C-tor.
///
/// The value represented by this structure.
public Optional(T value)
{
_value = value;
HasValue = true;
}
#endregion
#region ===================================================================== METHODS ===================================================================================
///
/// Custom implementation operator for converting to an .
///
/// The value to be converted.
public static implicit operator Optional(T value)
{
return new Optional(value);
}
#endregion
}