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

Add support for basic type inference within lambda functions #936

Merged
merged 23 commits into from May 17, 2021

Conversation

acizmarik
Copy link
Member

@acizmarik acizmarik commented Feb 9, 2021

This PR adds support for type inference within lambda functions.

Overview

We try to infer information about lambdas based on provided arguments and context in which the lambda is being used. Current biggest limitation is that we do not take into account lambda bodies. Still, in most cases, this simpler approach should work and infer correct type information. Following situations are covered by the type inferer:

  1. Lambdas as method arguments
    • Gather usable method candidates
    • Based on other method arguments, resolve open generic types and refine method candidates (in each iteration we have more accurate information about the method call)
    • Based on refined method candidates and resolved generics, try infer lambda type
  2. Top-level lambdas, such as SortChanged on grids
    • Here we must rely on the computed binding's expected type
  3. Compilation into Action<..> vs Func<..> vs Predicate<>
    • Originally presented as Fixed issue with lambdas and System.Action #995 (abandoned)
    • DotVVM distinguishes 2 statements: assignment and method invocation
    • If user specified a valid statement as lambda body and lambda should be assigned into an Action<..>, return value gets discarded and as such the compilation result can be treated as an Action<..>
    • This implementation uses type inference to decide whether we need to discard return value or not

Breaking change

Interface IBindingExpressionBuilder was altered by adding an optional parameter to its Parse method. Regardless, I do not think that this change could actually break someone

@acizmarik acizmarik added this to the Version 3.0 milestone Feb 9, 2021
@quigamdev quigamdev requested a review from exyi February 19, 2021 17:05
@tomasherceg tomasherceg modified the milestones: Version 3.0, Version 3.0 Nice to have Feb 21, 2021
Base automatically changed from feature/lambdas-extension-methods to main February 23, 2021 15:14
@tomasherceg tomasherceg modified the milestones: After 3.0, Version 3.1 Mar 27, 2021
@acizmarik acizmarik marked this pull request as ready for review April 20, 2021 12:10
@acizmarik
Copy link
Member Author

I added a test that showcases this feature works with methods introduced by #965. It should work nicely together with method calls, but also with extension method calls.

acizmarik and others added 2 commits May 13, 2021 10:42
@acizmarik acizmarik merged commit 1385534 into main May 17, 2021
@acizmarik acizmarik deleted the feature/lambdas-type-inference branch May 17, 2021 07:46
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

3 participants