2021-11-27 02:10:56 +00:00
|
|
|
namespace Myriad.Utils;
|
|
|
|
|
|
|
|
public interface IOptional
|
2020-12-24 13:52:44 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
object? GetValue();
|
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public readonly struct Optional<T>: IOptional
|
|
|
|
{
|
|
|
|
public Optional(T value)
|
2020-12-24 13:52:44 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
HasValue = true;
|
|
|
|
Value = value;
|
|
|
|
}
|
2020-12-24 13:52:44 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public bool HasValue { get; }
|
|
|
|
public object? GetValue() => Value;
|
2020-12-24 13:52:44 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public T Value { get; }
|
2020-12-24 13:52:44 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public static implicit operator Optional<T>(T value) => new(value);
|
2020-12-24 13:52:44 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public static Optional<T> Some(T value) => new(value);
|
|
|
|
public static Optional<T> None() => default;
|
2020-12-24 13:52:44 +00:00
|
|
|
}
|