PluralKit/Myriad/Utils/Optional.cs

25 lines
505 B
C#
Raw Normal View History

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