PluralKit/Myriad/Utils/Optional.cs

26 lines
579 B
C#
Raw Normal View History

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