Skip to content

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft

Conversation

Tyrrx
Copy link
Member

@Tyrrx Tyrrx commented Jan 31, 2025

No description provided.

@Tyrrx Tyrrx requested a review from ax0l0tl January 31, 2025 08:31
@@ -79,6 +84,16 @@ public async Task<TResult> Match<TResult>(Func<T, Task<TResult>> some, Func<Task

return await none().ConfigureAwait(false);
}

Copy link
Member

@ax0l0tl ax0l0tl Jan 31, 2025

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...

Copy link
Member Author

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)
Copy link
Collaborator

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);
Copy link
Collaborator

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

Suggested change
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants