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

Bugfix - underscore dot lambda conversion to Func<> in LINQ #16339

Conversation

T-Gro
Copy link
Member

@T-Gro T-Gro commented Nov 24, 2023

Fixes #16305

This changes how dot lambda is being recognized for type inference, making sure it is treated like a function type.
With that, it will be detected in method call scenarios for a possible conversion between function type to Func<,>.

The following three now work (before, only the first did):

open System.Linq
let _ = [""; ""; ""].Select(fun x -> x.Length)
let _ = [""; ""; ""].Select(_.Length)
let _ = [""; ""; ""].Select _.Length

@T-Gro T-Gro requested a review from a team as a code owner November 24, 2023 14:53
@DedSec256
Copy link
Contributor

Wouldn't this fix also allow conversion between a short lambda and System.Linq.Expressions.Expression<Func<...>>?

@T-Gro
Copy link
Member Author

T-Gro commented Nov 24, 2023

It should, let me add a simple test for that as well.

@auduchinok
Copy link
Member

Wouldn't this fix also allow conversion between a short lambda and System.Linq.Expressions.Expression<Func<...>>?

@DedSec256 Do you have a sample to add a test?

@DedSec256
Copy link
Contributor

open System
open System.Linq.Expressions

type A = static member M(x: Expression<Func<string, int>>) = ()

A.M(fun x -> x.Length)
A.M(_.Length)

Should we allow such a conversion? Will it be able to convert the F# expression into a tree at runtime?

@auduchinok
Copy link
Member

Should we allow such a conversion?

It should be the same as the existing behaviour for the existing lambda expressions. 🙂

@T-Gro
Copy link
Member Author

T-Gro commented Nov 24, 2023

Confirmed, it works.

@T-Gro
Copy link
Member Author

T-Gro commented Nov 24, 2023

Also it works without parens when using underscore dot lambda.

@vzarytovskii vzarytovskii enabled auto-merge (squash) November 24, 2023 18:30
@Tarmil
Copy link
Contributor

Tarmil commented Jan 13, 2024

I've done some testing with SDK 8.0.101, and while it works with Func<_, _>, it doesn't seem to be fully working with Expression<Func<_, _>>. It's quite weird actually.

  • [1].AsQueryable().Select(_.ToString()) works and does call the Expression version of Select.
  • ["1"].AsQueryable().Select(_.Length) gives "Error FS0072 : Lookup on object of indeterminate type based on information prior to this program point." (EDIT: it also does this without AsQueryable, ie when using Func and not Expression<Func>)
  • @DedSec256's snippet (calling a custom method that takes an Expression) gives "Error FS0002 : This function takes too many arguments, or is used in a context where a function is not expected".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Unexpected type inference error in shorthand lambda
6 participants