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

Accept an errorFactory when converting Maybe to Result #524

Open
blalonde opened this issue Nov 1, 2023 · 1 comment
Open

Accept an errorFactory when converting Maybe to Result #524

blalonde opened this issue Nov 1, 2023 · 1 comment

Comments

@blalonde
Copy link

blalonde commented Nov 1, 2023

Would this be possible ?

using System;

namespace CSharpFunctionalExtensions
{
    public static partial class MaybeExtensions
    {
        public static Result<T, E> ToResult<T, E>(in this Maybe<T> maybe, Func<E> errorFactory)
        {
            if (maybe.HasNoValue)
                return Result.Failure<T, E>(errorFactory());

            return Result.Success<T, E>(maybe.GetValueOrThrow());
        }
    }
}

It would avoid calling expensive error building code when maybe has a value.

@vkhorikov
Copy link
Owner

Sorry for the late answer. Yeah, the overload looks good to me.

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

2 participants