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

Allow calling extension methods on this as receiver #3338

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dembanakh
Copy link

Addresses #3334

Currently in Javac, whenever the call receiver is this, it is not considered as a candidate for extension method call.

if ("this".equals(receiver.toString()) || "this".equals(methodName) || "super".equals(methodName)) return;

This PR makes the behaviour consistent with that of Eclipse's handler, I believe. In other words, explicit this call would be considered as a possible extension method call.

@dembanakh
Copy link
Author

@Rawi01 Could you take a look at this by any chance? 😃

Copy link
Collaborator

@Rawi01 Rawi01 left a comment

Choose a reason for hiding this comment

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

  • Please add your name to the AUTHORS file
  • It would be great if you can rebase this branch on the current master. We recently enabled GitHub Actions to run the whole test suite and that way we do not have to run the tests locally. (Some eclipse test will fail but you can ignore that)

I'm not sure if extension methods should ever work like that but it should at least be consistent. It feels a little bit weird because you could simply add the method to your class instead of using extension method at all.

@dembanakh
Copy link
Author

Thanks, I fixed the failing test and rebased.

4 test suites are failing (the same 4 that fail on master), but the others pass successfully, so I hope that's alright.

While it indeed feels strange to call an extension method on this, I think this option is worth keeping. Consider e.g. the case where you have to use the extension method both inside and outside the receiver's class

@ExtensionMethod(Extensions.class)
class A {
  public void reset() {
    this.clear();
    // ...
  }
}

@ExtensionMethod(Extensions.class) 
class B {
  private A a;

  public void run() {
    a.clear();
    // ...
  }
}

class Extensions {
  public static void clear(A a) {}
}

@Rawi01
Copy link
Collaborator

Rawi01 commented Mar 27, 2023

In your example you could also add the clear method to A but this still might be useful if the base class is a third party/framework class.

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

2 participants