Skip to content

Add or else methods to option closes #31#47

Draft
Tyrrx wants to merge 2 commits intomainfrom
feature/or-else
Draft

Add or else methods to option closes #31#47
Tyrrx wants to merge 2 commits intomainfrom
feature/or-else

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

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.

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
Member

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
Member

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