Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Functional.Maybe.Tests/EitherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
47 changes: 23 additions & 24 deletions Functional.Maybe/Either.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,37 @@ public readonly struct Either<TResult, TError>
{
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;
}

/// <summary>
/// Constructs new <see cref="Either{TResult, TError}"/> with the Result part defined.
/// </summary>
public static Either<TResult, TError> Result(TResult result)
{
return new Either<TResult, TError>(result, default, true);
return new Either<TResult, TError>(result);
}

/// <summary>
/// Constructs new <see cref="Either{TResult, TError}"/> with the Error part defined.
/// </summary>
public static Either<TResult, TError> Error(TError error)
{
return new Either<TResult, TError>(default, error, false);
return new Either<TResult, TError>(error);
}

/// <summary>
Expand All @@ -60,25 +59,25 @@ public T Match<T>(Func<TResult, T> resultFunc, Func<TError, T> errorFunc)
throw new ArgumentNullException(nameof(errorFunc));
}

return _success ? resultFunc(_resultValue) : errorFunc(_errorValue);
return Success ? resultFunc(_resultValue) : errorFunc(_errorValue);
}

/// <summary>
/// Executes result or error function depending on the Either state.
/// </summary>
public T Match<T>(Func<T> leftFunc, Func<T> rightFunc)
public T Match<T>(Func<T> resultFunc, Func<T> 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();
}

/// <summary>
Expand All @@ -96,7 +95,7 @@ public void Match(Action<TResult> resultAction, Action<TError> errorAction)
throw new ArgumentNullException(nameof(errorAction));
}

if (_success)
if (Success)
{
resultAction(_resultValue);
}
Expand All @@ -121,7 +120,7 @@ public void Match(Action resultAction, Action errorAction)
throw new ArgumentNullException(nameof(errorAction));
}

if (_success)
if (Success)
{
resultAction();
}
Expand Down