Skip to content
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

ValueOr and AsMaybe for results #594

Open
RorySan opened this issue Jan 10, 2025 · 0 comments
Open

ValueOr and AsMaybe for results #594

RorySan opened this issue Jan 10, 2025 · 0 comments

Comments

@RorySan
Copy link

RorySan commented Jan 10, 2025

Hello, I wrote two simple extension methods that are helping me in my project, but I'm guessing there is probably a better alternative already built in that I am not using:

    public static Maybe<T> AsMaybe<T>(this Result<T> result) =>
        result.IsSuccess ? result.Value : Maybe<T>.None;
    
    public static string ValueOr<T>(this Maybe<T> maybe, string alternativeValue) 
        where T : SimpleValueObject<string> => 
        maybe.HasValue ? maybe.Value : alternativeValue;

I found a way of using the existing Or to achieve this, but it is a bit more verbose. With this ValueOr i get to write things like Email.ValueOr(string.Empty) which is handy when displaying optional info on the UI. Am I missing a better way of doing this?

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

No branches or pull requests

1 participant