Skip to content
Merged
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
26 changes: 25 additions & 1 deletion EasyMonads/Either/Either.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ private static void ValidateFunction<T1, T2>(Func<T1, T2> function)
throw new ArgumentNullException(nameof(function));
}
}

private static void ValidateMatch<TL, TR>(Func<TLeft, TL> left, Func<TRight, TR> right)
{
if (left is null)
Expand All @@ -173,6 +173,16 @@ private static void ValidateMatch<TL, TR>(Func<TLeft, TL> left, Func<TRight, TR>
}
}

private static void ValidateMatch<TN, TL, TR>(Func<TN> neither, Func<TLeft, TL> left, Func<TRight, TR> right)
{
if (neither is null)
{
throw new ArgumentNullException(nameof(neither));
}

ValidateMatch(left, right);
}

public TResult Match<TResult>(Func<TLeft, TResult> left, Func<TRight, TResult> right, TResult neither)
{
ValidateMatch(left, right);
Expand All @@ -187,6 +197,20 @@ public TResult Match<TResult>(Func<TLeft, TResult> left, Func<TRight, TResult> r
#pragma warning restore CS8524
}

public TResult Match<TResult>(Func<TLeft, TResult> left, Func<TRight, TResult> right, Func<TResult> neither)
{
ValidateMatch(neither, left, right);

#pragma warning disable CS8524
return _state switch
{
EitherState.Neither => neither(),
EitherState.Left => left(_left!),
EitherState.Right => right(_right!)
};
#pragma warning restore CS8524
}

public TResult Match<TResult>(TResult leftOrNeither, Func<TRight, TResult> right)
{
ValidateFunction(right);
Expand Down
Loading