From fc916c10de72262fd5d96cdd3d73dbe232f584d8 Mon Sep 17 00:00:00 2001 From: Sanchime Date: Fri, 13 May 2022 22:18:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=87=BD=E6=95=B0=E5=BC=8F?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sanchime.Functional/Products/Exceptional.cs | 37 +++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Sanchime.Functional/Products/Exceptional.cs 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})"); + +}