Skip to content

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

Merged
vzarytovskii merged 4 commits into
dotnet:mainfrom
T-Gro:16305-unexpected-type-inference-error-in-shorthand-lambda
Nov 24, 2023
Merged

Bugfix - underscore dot lambda conversion to Func<> in LINQ#16339
vzarytovskii merged 4 commits into
dotnet:mainfrom
T-Gro:16305-unexpected-type-inference-error-in-shorthand-lambda

Conversation

@T-Gro

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

Copy link
Copy Markdown
Member

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
Copy Markdown
Contributor

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

@T-Gro

T-Gro commented Nov 24, 2023

Copy link
Copy Markdown
Member Author

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

@auduchinok

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member

Should we allow such a conversion?

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

@T-Gro

T-Gro commented Nov 24, 2023

Copy link
Copy Markdown
Member Author

Confirmed, it works.

@T-Gro

T-Gro commented Nov 24, 2023

Copy link
Copy Markdown
Member Author

Also it works without parens when using underscore dot lambda.

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

Tarmil commented Jan 13, 2024

Copy link
Copy Markdown
Contributor

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