Accept an errorFactory when converting Maybe to Result
blalonde opened this issue · comments
blalonde commented
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.
Vladimir Khorikov commented
Sorry for the late answer. Yeah, the overload looks good to me.