Skip to content

ValueOr and AsMaybe for results #594

@RorySan

Description

@RorySan

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions