vkhorikov / CSharpFunctionalExtensions

Functional extensions for C#

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Accept an errorFactory when converting Maybe to Result

blalonde opened this issue · comments

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.

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