-
Notifications
You must be signed in to change notification settings - Fork 3
Add or else methods to option closes #31 #82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -59,6 +59,11 @@ public Task<Option<T1>> Map<T1>(Func<T, Task<T1>> map) => Match( | |||||||||||||||||||||||||||||||||||||
| public Option<T1> Bind<T1>(Func<T, Option<T1>> map) => Match(map, Option<T1>.None); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| public Task<Option<T1>> Bind<T1>(Func<T, Task<Option<T1>>> bind) => Match(bind, () => Option<T1>.None); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| public Option<T> OrElse(Option<T> other) => _isSome ? this : other; | ||||||||||||||||||||||||||||||||||||||
| public Option<T> OrElse(Func<Option<T>> other) => _isSome ? this : other(); | ||||||||||||||||||||||||||||||||||||||
| public Task<Option<T>> OrElse(Task<Option<T>> other) => OrElse(() => other); | ||||||||||||||||||||||||||||||||||||||
| public async Task<Option<T>> OrElse(Func<Task<Option<T>>> other) => _isSome ? this : await other().ConfigureAwait(false); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| public void Match(Action<T> some, Action? none = null) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -90,6 +95,8 @@ public async Task<TResult> Match<TResult>(Func<T, Task<TResult>> some, Func<Task | |||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| return await none().ConfigureAwait(false); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+98
to
+99
|
||||||||||||||||||||||||||||||||||||||
Copilot
AI
Mar 10, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Task<Option<T>> extension overload OrElse(..., Task<Option<T>> other) has the same unobserved-exception risk as the instance overload: if the awaited option is Some, other is never awaited, so faults may go unobserved. Prefer a Func<Task<Option<T>>> overload only, or observe other's exceptions when it isn't needed.
| public static async Task<Option<T>> OrElse<T>(this Task<Option<T>> option, Task<Option<T>> other) | |
| { | |
| var result = await option.ConfigureAwait(false); | |
| return await result.OrElse(other).ConfigureAwait(false); | |
| public static Task<Option<T>> OrElse<T>(this Task<Option<T>> option, Task<Option<T>> other) | |
| { | |
| if (other == null) throw new ArgumentNullException(nameof(other)); | |
| // Ensure any exception on 'other' is observed even if 'option' is Some and short-circuits. | |
| _ = other.ContinueWith( | |
| t => | |
| { | |
| var _ = t.Exception; | |
| }, | |
| TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously); | |
| // Delegate to the Func<Task<Option<T>>> overload to preserve existing behavior. | |
| return option.OrElse(() => other); |
Copilot
AI
Mar 10, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line appears to have trailing whitespace at the end. Please trim it to avoid noisy diffs and potential formatting/lint issues.
| items.SelectMany(i => choose(i)); | |
| items.SelectMany(i => choose(i)); |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -828,6 +828,23 @@ public void NoneIfEmpty_Text_Some() | |||||||||||||||||||||||||||||||||||||||
| string? target = "Hi"; | ||||||||||||||||||||||||||||||||||||||||
| var result = target.NoneIfEmpty(); | ||||||||||||||||||||||||||||||||||||||||
| result.Should().BeSome().Which.Should().Be("Hi"); | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| [TestMethod] | ||||||||||||||||||||||||||||||||||||||||
| public async Task OrElse() | ||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||
| var none = None<int>(); | ||||||||||||||||||||||||||||||||||||||||
| none.OrElse(Option<int>.None).Should().BeNone(); | ||||||||||||||||||||||||||||||||||||||||
| ShouldBeSome42(none.OrElse(42)); | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| var some = Some(42); | ||||||||||||||||||||||||||||||||||||||||
| ShouldBeSome42(some.OrElse(Option<int>.None)); | ||||||||||||||||||||||||||||||||||||||||
| ShouldBeSome42(some.OrElse(2)); | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| ShouldBeSome42(await none.OrElse(() => Task.FromResult(Some(42)))); | ||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
| return; |
Copilot
AI
Mar 10, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This new test only exercises the synchronous Option<T>.OrElse(Option<T>), the implicit-value case, and the Func<Task<Option<T>>> overload. It doesn't cover OrElse(Func<Option<T>>) or the Task-based overloads (including the Task<Option<T>> extension methods). Adding assertions for those overloads would prevent regressions in the newly added API surface.
| ShouldBeSome42(await none.OrElse(() => Task.FromResult(Some(42)))); | |
| return; | |
| // Async overload taking Func<Task<Option<T>>> | |
| ShouldBeSome42(await none.OrElse(() => Task.FromResult(Some(42)))); | |
| // Synchronous overload taking Func<Option<T>> | |
| ShouldBeSome42(none.OrElse(() => Some(42))); | |
| // For Some, the factory should not be invoked; value should stay 42 | |
| ShouldBeSome42(some.OrElse(() => Some(100))); | |
| // Task-based overloads on Option<T> | |
| ShouldBeSome42(await none.OrElse(Task.FromResult(Some(42)))); | |
| // Task<Option<T>> extension method overloads | |
| var noneTask = Task.FromResult(none); | |
| ShouldBeSome42(await noneTask.OrElse(Some(42))); | |
| ShouldBeSome42(await noneTask.OrElse(() => Some(42))); | |
| ShouldBeSome42(await noneTask.OrElse(Task.FromResult(Some(42)))); | |
| ShouldBeSome42(await noneTask.OrElse(() => Task.FromResult(Some(42)))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OrElse(Task<Option<T>> other)can leaveotherrunning without being awaited when the current option isSome. Ifotherfaults, the exception can become unobserved (and may surface viaTaskScheduler.UnobservedTaskException). Consider removing this overload in favor ofOrElse(Func<Task<Option<T>>>), or ensure exceptions fromotherare observed even when it isn't used (e.g., via a continuation).