CSharpFunctionalExtensions
CSharpFunctionalExtensions copied to clipboard
Add Func<> equivalent to Execute Action<> Maybe extension
Great library, love the approach and your Enterprise Craftsmanship series 😊
When using the library in my own project I found that I sometimes wanted to return a value from the Maybe<>.Execute() extension method, so I've added a Func<> overload in this PR in the hope that it might be of use to others.
Alternatively, the following is more comprehensive but extends Nullable<<T>> as well. I didn't know if you wanted to go that far...
public static Maybe<TOut> Execute<TIn, TOut>(this Maybe<TIn> maybe, Func<TIn, TOut> func)
where TIn:class
where TOut:class
{
if (maybe.HasValue)
return func(maybe.Value);
else
return null;
}
public static TOut? Execute<TIn, TOut>(this Maybe<TIn> maybe, Func<TIn, TOut?> func)
where TIn : class
where TOut : struct
{
if (maybe.HasValue)
return func(maybe.Value);
else
return null;
}
public static Maybe<TOut> Execute<TIn, TOut>(this TIn? nullable, Func<TIn, TOut> func)
where TIn : struct
where TOut : class
{
if (nullable.HasValue)
return func(nullable.Value);
else
return null;
}
public static TOut? Execute<TIn, TOut>(this TIn? nullable, Func<TIn, TOut?> func)
where TIn : struct
where TOut : struct
{
if (nullable.HasValue)
return func(nullable.Value);
else
return null;
}
Thanks for the contribution!
Did you consider the Select extension method? It seems like it does exactly what you need (transforms a Maybe<T1>
to a Maybe<T2>
).
I see now that the naming I've chosen is probably confusing. Do you think both Execute
and Select
should be named the same way? If so, which name should we pick?
Regarding extensions for structs - I like the idea!
I did completely miss that 😞 I was looking for something similar to OnSuccess() for Result<>. I think Select is the logical choice, I'll work with that for now.
Still great suggestion regarding structs! I'll add it soon and will roll out a new version of the lib.
As overloads to Select? I'm happy to amend the PR if that helps?
As overloads to Select?
Yes.
I'm happy to amend the PR if that helps?
Would be very helpful, thanks!
So I've run into a blocker here.
The methods
public static Maybe<K> Select<T, K>(this Maybe<T> maybe, Func<T, K> selector)
where T : class
where K : class
and
public static K? Select<T, K>(this Maybe<T> maybe, Func<T, K> selector)
where T : class
where K : struct
have the same signature. Despite the different type constraints. More info here https://blogs.msdn.microsoft.com/ericlippert/2009/12/10/constraints-are-not-part-of-the-signature/
The solution to this isn't immediately apparent to me 😕
(My original code had Func<T, K?> selector
, but of course this isn't right - the func itself should not return a nullable)
Ah, indeed, return values also don't count as overloads.
Maybe we can rename Select
for structs to some other method? How about SelectStruct
?
@laurence79 Is this PR still active? Please, fix the merge conflicts or say something :)