using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; namespace SJK.Functional; public class Some : IOption { private readonly T _data; private Some(T data) { _data = data; } public ref readonly T Value => ref _data; public static Some Of(T data) => new(data); // [MethodImpl(MethodImplOptions.AggressiveInlining)]//TODO see if these inporve perfomce in gernral cases public TResult Match(Func onSome, Func _) => onSome(_data); // [MethodImpl(MethodImplOptions.AggressiveInlining)] public IOption Bind(Func> f) => f(_data); // [MethodImpl(MethodImplOptions.AggressiveInlining)] public IOption Map(Func f) => new Some(f(_data)); // [MethodImpl(MethodImplOptions.AggressiveInlining)] public T Or(T _) => _data; public T Or(Func aDefault) => _data; public bool HasValue([NotNullWhen(true)]out T? value) => (value = _data) is not null; public bool HasValue() => true; // [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Match(Action onSome, Action onNone) => onSome(_data); public override string ToString() { return nameof(Some)+": "+ _data; } public bool Equals(IOption? other) { if (other is Some some){ return _data!.Equals(some._data); } return false; } }