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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Func<> equivalent to Execute Action<> Maybe extension #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

laurence79
Copy link

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.

@laurence79
Copy link
Author

laurence79 commented Sep 13, 2016

Alternatively, the following is more comprehensive but extends Nullable<> 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;
}

@vkhorikov
Copy link
Owner

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!

@laurence79
Copy link
Author

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.

@vkhorikov
Copy link
Owner

Still great suggestion regarding structs! I'll add it soon and will roll out a new version of the lib.

@laurence79
Copy link
Author

As overloads to Select? I'm happy to amend the PR if that helps?

@vkhorikov
Copy link
Owner

vkhorikov commented Sep 13, 2016

As overloads to Select?

Yes.

I'm happy to amend the PR if that helps?

Would be very helpful, thanks!

@laurence79
Copy link
Author

laurence79 commented Sep 14, 2016

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)

@vkhorikov
Copy link
Owner

Ah, indeed, return values also don't count as overloads.
Maybe we can rename Select for structs to some other method? How about SelectStruct?

@@ -69,5 +69,14 @@ public static void Execute<T>(this Maybe<T> maybe, Action<T> action)

action(maybe.Value);
}

public static TOut Execute<T, TOut>(this Maybe<T> maybe, Func<T, TOut> func)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like Map method, not like Execute or Select

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Select == Map in C#. Found some conflicts merging this PR. Could you re-submit?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@a-z-hub Ping!
Is this still active?

@SuperJMN
Copy link

SuperJMN commented Nov 7, 2021

@laurence79 Is this PR still active? Please, fix the merge conflicts or say something :)

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

Successfully merging this pull request may close these issues.

None yet

4 participants