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

SemanticallyEqual should allow for different lambda parameter names #4494

Merged
merged 2 commits into from
Sep 15, 2024

Conversation

knutwannheden
Copy link
Contributor

@knutwannheden knutwannheden commented Sep 15, 2024

While comparing two lambdas for semantical equality, the lambda parameter names should be ignored. This actually also applies to other variables (e.g. variables declared in a block such as a lambda body), but that is out of scope for this PR.

This will allow Refaster recipes to match a lambda against other lambdas using different parameter names.

Caused a minor test attribution error.
While comparing two lambdas for semantical equality, the lambda parameter names should be ignored. This actually also applies to other variables (e.g. variables declared in a block such as a lambda body), but that is out of scope for this PR.

This will allow Refaster recipes to match a lambda against other lambdas using different parameter names.
@knutwannheden knutwannheden changed the title semantically equal lambdas SemanticallyEqual should allow for different lambda parameter names Sep 15, 2024
@knutwannheden
Copy link
Contributor Author

knutwannheden commented Sep 15, 2024

@timtebeek I was looking into supporting lambdas in Refaster recipes. While we still can't support templates directly returning lambdas, we can support templates "using" lambdas. So while the following can't be supported, because JavaTemplate doesn't allow us to provide the type of the lambda:

        @BeforeTemplate
        public Comparator<Integer> before() {
            return (x, y) -> x - y;
        }

        @AfterTemplate
        public Comparator<Integer> after() {
            return Comparator.comparingInt(x -> x);
        }

something like this we could support:

        @BeforeTemplate
        public void before(List<Integer> l) {
            l.sort((x, y) -> x - y);
        }

        @AfterTemplate
        public void after(List<Integer> l) {
            l.sort(Comparator.comparingInt(x -> x));
        }

In this particular example that would of course be very unfortunate, as replacing the (x, y) -> x - y lambda (with type Comparator<Integer>) with Comparator.comparingInt(x -> x) would always make sense, regardless of where it is used. But I think there are still enough other use cases, when supporting lambdas when part of a method invocation would already be a very useful addition.

This PR then only makes sure that we can then use JavaTemplate.match() to match a lambda which uses different parameter names. In the example above we also want to match l.sort((a, b) -> a - b); for example.

It would then be nice if we at some point can also extend this to also support templates which directly return a lambda.

@knutwannheden
Copy link
Contributor Author

knutwannheden commented Sep 15, 2024

Copy link
Contributor

@timtebeek timtebeek left a comment

Choose a reason for hiding this comment

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

Looks good! Thanks for taking the time to expand what we can cover with Refaster.

@knutwannheden knutwannheden merged commit 0535297 into main Sep 15, 2024
2 checks passed
@knutwannheden knutwannheden deleted the semantically-equal-lambdas branch September 15, 2024 09:54
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.

2 participants