添加函数式异常类
This commit is contained in:
parent
e8a5dd982d
commit
fc916c10de
|
@ -0,0 +1,37 @@
|
||||||
|
namespace Sanchime.Functional.Products;
|
||||||
|
|
||||||
|
public readonly struct Exceptional<TValue>
|
||||||
|
{
|
||||||
|
internal Exception _exception { get; }
|
||||||
|
|
||||||
|
internal TValue _value { get; }
|
||||||
|
|
||||||
|
public bool Success => _exception is null;
|
||||||
|
|
||||||
|
public bool Exception => !Success;
|
||||||
|
|
||||||
|
internal Exceptional(Exception exception)
|
||||||
|
{
|
||||||
|
if (exception == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(exception));
|
||||||
|
}
|
||||||
|
(_exception, _value) = (exception, default!);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal Exceptional(TValue right)
|
||||||
|
=> (_value, _exception) = (right, default!);
|
||||||
|
|
||||||
|
public static implicit operator Exceptional<TValue>(Exception left)
|
||||||
|
=> new(left);
|
||||||
|
|
||||||
|
public static implicit operator Exceptional<TValue>(TValue right)
|
||||||
|
=> new(right);
|
||||||
|
|
||||||
|
public TRight Match<TRight>(Func<Exception, TRight> Exception, Func<TValue, TRight> Success)
|
||||||
|
=> this.Exception ? Exception(_exception) : Success(_value);
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
=> Match(e => $"Exception({e.Message})", v => $"Success({v})");
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue