diff --git a/Sanchime.Functional/Products/Exceptional.cs b/Sanchime.Functional/Products/Exceptional.cs new file mode 100644 index 0000000..4dd4f7a --- /dev/null +++ b/Sanchime.Functional/Products/Exceptional.cs @@ -0,0 +1,37 @@ +namespace Sanchime.Functional.Products; + +public readonly struct Exceptional +{ + 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(Exception left) + => new(left); + + public static implicit operator Exceptional(TValue right) + => new(right); + + public TRight Match(Func Exception, Func Success) + => this.Exception ? Exception(_exception) : Success(_value); + + public override string ToString() + => Match(e => $"Exception({e.Message})", v => $"Success({v})"); + +}