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

Question: How to use linq query syntax for Result types #566

Open
christophercalm opened this issue Sep 17, 2024 · 6 comments
Open

Question: How to use linq query syntax for Result types #566

christophercalm opened this issue Sep 17, 2024 · 6 comments

Comments

@christophercalm
Copy link

Is the Select method not supported for linq query syntax for result?

As an example here is code that works with Maybe

Maybe<string> name = "John";

var upper = from x in name select x.ToUpper();`

But does not work with Result

This is the error:
CS1936 Could not find an implementation of the query pattern for source type 'Result'. 'Select' not found.

I see the that SelectMany operation is supported but it looks like there is no select. Is this something that you would be open to me adding?

It would help with workflows like this

Result<string> GetGreetingMessage(string userName)
{
    var maybeGreeting =
        from name in ValidateName(userName)   // Bind the validated name
        select CreateGreetingMessage(name);   // Map to a greeting message

    // Convert Maybe to Result
    return maybeGreeting.ToResult("Invalid name provided");
}

Maybe<string> ValidateName(string name)
{
    // Check if the name is valid, return Maybe.None if invalid
    return string.IsNullOrWhiteSpace(name) ? Maybe<string>.None : Maybe<string>.From(name);
}

but for results.

@vkhorikov
Copy link
Owner

We're trying to keep naming consistent in this library. In the earlier days there were discussions on which naming approach to choose: the C# one (Select, SelectMany, etc) or the F# one (Map, 'Bind', etc). We decided to follow the F# naming convention because it's more consistent (e.g SelectMany doesn't really make sense for Result and even for Maybe).

So all Select extensions remaining should really be marked as obsolete and redirected to Map.

The downside to this decision is that Linq expressions (from ... in ... select) won't work anymore, but people rarely use those. And I suggest to just use regular linq instead.

@christophercalm
Copy link
Author

The reason for using linq expressions is that you can combine results from many different monads. You can’t do that with the regular linq syntax without multiple select many expressions. I would ask that you please reconsider as the rest of the api is complete in regards to the overloads for linq query expressions.

@vkhorikov
Copy link
Owner

OK, makes sense. Feel free to submit a PR. A couple of implementation notes:

  1. Select should call Map internally; SelectMany -- Bind (just so that we don't have duplicate implementations)
  2. Please leave a note as to why we have Select and SelectMany -- to support linq expressions

@christophercalm
Copy link
Author

OK, makes sense. Feel free to submit a PR. A couple of implementation notes:

  1. Select should call Map internally; SelectMany -- Bind (just so that we don't have duplicate implementations)
  2. Please leave a note as to why we have Select and SelectMany -- to support linq expressions

9a80bf2

That has been done in this commit.

@vkhorikov
Copy link
Owner

Would you be able to raise a PR for this commit? Just select your branch here: https://github.com/vkhorikov/CSharpFunctionalExtensions/compare and hit "Create pull request"

@christophercalm
Copy link
Author

christophercalm commented Oct 2, 2024

Would you be able to raise a PR for this commit? Just select your branch here: https://github.com/vkhorikov/CSharpFunctionalExtensions/compare and hit "Create pull request"

I have one here. :)

#567

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