Add terminal Else overloads for FailOr<T> and Task<FailOr<T>>#8
Merged
Add terminal Else overloads for FailOr<T> and Task<FailOr<T>>#8
Else overloads for FailOr<T> and Task<FailOr<T>>#8Conversation
Co-authored-by: mark-pro <20671988+mark-pro@users.noreply.github.com>
Co-authored-by: mark-pro <20671988+mark-pro@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Add terminal Else overloads for FailOr<T> and Task<FailOr<T>>
Add terminal Mar 9, 2026
Else overloads for FailOr<T> and Task<FailOr<T>>
mark-pro
approved these changes
Mar 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds a dedicated terminal
ElseAPI that collapsesFailOr<TSource>intoTSource: return the success value as-is, or produce a fallback value on failure. It complementsIfFailThenby covering the common “identity on success, fallback on failure” path without staying insideFailOr<T>.New terminal extension surface
/src/FailOr/FailOrT.Else.csFailOr<TSource>:Else(TSource alternative)Else(Func<TSource> alternative)Else(Func<IReadOnlyList<Failures>, TSource> alternative)ElseAsync(Func<Task<TSource>> alternativeAsync)ElseAsync(Func<IReadOnlyList<Failures>, Task<TSource>> alternativeAsync)Task<FailOr<TSource>>Behavior and guard semantics
Failuressequence in orderDedicated coverage
/tests/FailOr.Tests/FailOrElseTests.cs/tests/FailOr.Tests/TaskFailOrElseTests.cs/tests/FailOr.Tests/ElseTestData.csExample:
Original prompt
This section details on the original issue you should resolve
<issue_title>Add terminal Else overloads for FailOr and Task<FailOr></issue_title>
<issue_description>## Summary
Add an
ElseAPI surface that terminatesFailOr<TSource>intoTSourceby returning the successful value when present or an alternative value when the result is failed.This should complement the existing
IfFailThenrecovery APIs:IfFailThenkeeps the caller insideFailOr<TSource>Elseshould terminate toTSourceIt should also provide a lighter-weight alternative to
Matchfor the common "identity on success, fallback on failure" case.Scope
Implementation should be isolated to a new extension file and a complete dedicated test suite.
Required production file:
src/FailOr/FailOrT.Else.csAcceptable test files:
tests/FailOr.Tests/FailOrElseTests.cstests/FailOr.Tests/TaskFailOrElseTests.cstests/FailOr.Tests/ElseTestData.csNo other existing source files should need to be modified for this change.
Proposed API
Add a new extension class in
src/FailOr/FailOrT.Else.cs, following the existing extension-block pattern used byFailOrT.Then.csandFailOrT.Match.cs.Direct overloads for
FailOr<TSource>Lifted overloads for
Task<FailOr<TSource>>Behavioral Requirements
Elsemust return the wrapped success value unchanged.Elsemust return the immediate alternative or invoke the fallback delegate and return its result.Failuressequence in the same order exposed bysource.Failures.Task<FailOr<TSource>>overloads must match the behavior of the direct overloads after awaiting the source task.FailOrT.Then.csandFailOrT.Match.cs.Validation Requirements
Match the repository's current guard-clause behavior and parameter naming conventions.
Expected validation coverage:
ArgumentNullExceptionwhen a fallback delegate isnullArgumentNullExceptionwhen a liftedsourceTaskisnullArgumentNullExceptionwhen an async fallback delegate returnsnullDocumentation Requirements
Each new public function should include XML documentation comments with:
Test Requirements
Add a complete accompanying test suite that covers all direct and lifted overloads.
Minimum coverage:
nulltask throw with the expected parameter nameExample Usage
Acceptance Criteria
Elseexists as a dedicated extension surface insrc/FailOr/FailOrT.Else.csTSource, notFailOr<TSource>✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.