FruityFoundation/Base/Structures/Maybe.cs

107 lines
2.3 KiB
C#

namespace FruityFoundation.Base.Structures;
using System;
public static class Maybe
{
public static Maybe<T> Create<T>(T value) => new(value);
public static Maybe<T> Create<T>(T value, Func<bool> evalIsEmpty) =>
evalIsEmpty()
? Empty<T>()
: new Maybe<T>(value);
public static Maybe<T> Empty<T>() => new();
}
public readonly record struct Maybe<T>
{
private readonly T _value;
public bool HasValue { get; }
internal Maybe(T val) : this()
{
_value = val;
HasValue = true;
}
public T Value
{
get
{
if (!HasValue)
throw new InvalidOperationException($"{nameof(Maybe<T>)} value is empty");
return _value;
}
}
public T OrValue(T orVal) => HasValue ? Value : orVal;
public T OrEval(Func<T> valueFactory) => HasValue ? Value : valueFactory();
public bool Try(out T val)
{
val = HasValue ? Value : default!;
return HasValue;
}
public T OrThrow(string msg) =>
HasValue ? Value : throw new Exception(msg);
public T OrThrow(Func<string> messageFactory) =>
HasValue ? Value : throw new Exception(messageFactory());
public T OrThrow(Func<Exception> exFactory) =>
HasValue ? Value : throw exFactory();
public Maybe<TOutput> Map<TOutput>(Func<T, TOutput> transformer) =>
HasValue ? Maybe.Create(transformer(Value)) : Maybe.Empty<TOutput>();
public Maybe<TOutput> Bind<TOutput>(Func<T, Maybe<TOutput>> binder) =>
HasValue ? binder(Value) : Maybe.Empty<TOutput>();
public Maybe<T> EmptyBind(Func<Maybe<T>> maybeFactory) =>
HasValue ? this : maybeFactory();
public Maybe<T> EmptyBind(Maybe<T> maybe) =>
HasValue ? this : maybe;
public Maybe<TOutput> Cast<TOutput>()
{
if (!HasValue)
return Maybe.Empty<TOutput>();
try
{
var t = typeof(TOutput);
t = Nullable.GetUnderlyingType(t) ?? t;
var output = Value == null
? default
: (TOutput)Convert.ChangeType(Value, t);
if (output == null)
return Maybe.Empty<TOutput>();
return Maybe.Create(output);
}
catch (InvalidCastException)
{
return Maybe.Empty<TOutput>();
}
}
public static implicit operator Maybe<T>(T val) => Maybe.Create(val);
public static explicit operator T(Maybe<T> val) => val.Value;
public override string ToString()
{
return (Try(out var val)
? val?.ToString()
: base.ToString())!;
}
}