diff --git a/Functional.Maybe.Tests/EitherTests.cs b/Functional.Maybe.Tests/EitherTests.cs index 0f352fa..ef154ec 100644 --- a/Functional.Maybe.Tests/EitherTests.cs +++ b/Functional.Maybe.Tests/EitherTests.cs @@ -159,9 +159,11 @@ public void ExtensionMethodTests() Assert.AreEqual(29, eitherResult.ResultOrDefault()); Assert.AreEqual(null, eitherResult.ErrorOrDefault()); + Assert.IsTrue(eitherResult.Success); Assert.AreEqual("Twenty nine", eitherError.ErrorOrDefault()); - Assert.AreEqual(0, eitherError.ResultOrDefault()); + Assert.AreEqual(default, eitherError.ResultOrDefault()); + Assert.IsFalse(eitherError.Success); } } diff --git a/Functional.Maybe/Either.cs b/Functional.Maybe/Either.cs index 2d875e0..6b39d5c 100644 --- a/Functional.Maybe/Either.cs +++ b/Functional.Maybe/Either.cs @@ -11,22 +11,21 @@ public readonly struct Either { private readonly TResult _resultValue; private readonly TError _errorValue; - private readonly bool _success; - private Either(TResult result, TError error, bool success) + public bool Success { get; } + + private Either(TResult result) { - _success = success; + Success = true; + _errorValue = default; + _resultValue = result; + } - if (success) - { - _resultValue = result; - _errorValue = default; - } - else - { - _errorValue = error; - _resultValue = default; - } + private Either(TError error) + { + Success = false; + _errorValue = error; + _resultValue = default; } /// @@ -34,7 +33,7 @@ private Either(TResult result, TError error, bool success) /// public static Either Result(TResult result) { - return new Either(result, default, true); + return new Either(result); } /// @@ -42,7 +41,7 @@ public static Either Result(TResult result) /// public static Either Error(TError error) { - return new Either(default, error, false); + return new Either(error); } /// @@ -60,25 +59,25 @@ public T Match(Func resultFunc, Func errorFunc) throw new ArgumentNullException(nameof(errorFunc)); } - return _success ? resultFunc(_resultValue) : errorFunc(_errorValue); + return Success ? resultFunc(_resultValue) : errorFunc(_errorValue); } /// /// Executes result or error function depending on the Either state. /// - public T Match(Func leftFunc, Func rightFunc) + public T Match(Func resultFunc, Func errorFunc) { - if (leftFunc == null) + if (resultFunc == null) { - throw new ArgumentNullException(nameof(leftFunc)); + throw new ArgumentNullException(nameof(resultFunc)); } - if (rightFunc == null) + if (errorFunc == null) { - throw new ArgumentNullException(nameof(rightFunc)); + throw new ArgumentNullException(nameof(errorFunc)); } - return _success ? leftFunc() : rightFunc(); + return Success ? resultFunc() : errorFunc(); } /// @@ -96,7 +95,7 @@ public void Match(Action resultAction, Action errorAction) throw new ArgumentNullException(nameof(errorAction)); } - if (_success) + if (Success) { resultAction(_resultValue); } @@ -121,7 +120,7 @@ public void Match(Action resultAction, Action errorAction) throw new ArgumentNullException(nameof(errorAction)); } - if (_success) + if (Success) { resultAction(); }