32 lines
1.1 KiB
C#
32 lines
1.1 KiB
C#
|
|
using System.Diagnostics.CodeAnalysis;
|
||
|
|
using System.Runtime.CompilerServices;
|
||
|
|
|
||
|
|
namespace SJK.Functional;
|
||
|
|
|
||
|
|
public sealed class None<T> : IOption<T>
|
||
|
|
{
|
||
|
|
|
||
|
|
public static None<T> Of() => new();
|
||
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||
|
|
public TResult Match<TResult>(Func<T, TResult> _, Func<TResult> onNone) =>
|
||
|
|
onNone();
|
||
|
|
|
||
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||
|
|
public void Match(Action<T> onSome, Action onNone) => onNone();
|
||
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||
|
|
public IOption<TResult> Bind<TResult>(Func<T, IOption<TResult>> f) => new None<TResult>();
|
||
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||
|
|
public IOption<TResult> Map<TResult>(Func<T, TResult> f) => new None<TResult>();
|
||
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||
|
|
public T Or(T aDefault) => aDefault;
|
||
|
|
public T Or(Func<T> aDefault)=>aDefault();
|
||
|
|
public bool HasValue([NotNullWhen(true)]out T? value) => (value = default) is not null && false;
|
||
|
|
public bool HasValue() => false;
|
||
|
|
public override string ToString()
|
||
|
|
{
|
||
|
|
return nameof(None<T>);
|
||
|
|
}
|
||
|
|
|
||
|
|
public bool Equals(IOption<T>? other)=> other is None<T>;
|
||
|
|
}
|