-
Notifications
You must be signed in to change notification settings - Fork 2
Add or else methods to option closes #31 #47
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
base: main
Are you sure you want to change the base?
Conversation
@@ -79,6 +84,16 @@ public async Task<TResult> Match<TResult>(Func<T, Task<TResult>> some, Func<Task | |||
|
|||
return await none().ConfigureAwait(false); | |||
} | |||
|
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.
Is this actually a breaking change? -> GetValueOrDefault...
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.
You mean the match? In certain cases, as seen in GetValueOrDefault, it breaks the C# type inference.
@@ -181,22 +193,46 @@ public static async Task<Option<TOut>> Bind<T, TOut>(this Task<Option<T>> bind, | |||
var result = await bind.ConfigureAwait(false); | |||
return await result.Bind(convert).ConfigureAwait(false); | |||
} | |||
|
|||
public static async Task<Option<T>> OrElse<T>(this Task<Option<T>> option, Option<T> other) |
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 is one I have implemented by hand in some projects as well.
Other names we had in mind were .Chain
, so like you are chaining the options and take the first one that contains some, or .FallbackTo
but .OrElse
also sounds good
ShouldBeSome42(await none.OrElse(() => Task.FromResult(Some(42)))); | ||
return; | ||
|
||
void ShouldBeSome42(Option<int> option) => option.Should().BeSome().Subject.Should().Be(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.
.BeSome()
returns an AndWithConstraint<...>
, so you can say
void ShouldBeSome42(Option<int> option) => option.Should().BeSome().Subject.Should().Be(42); | |
void ShouldBeSome42(Option<int> option) => option.Should().BeSome().Which.Should().Be(42); |
and use .Which
No description provided.