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

Parameterless extension method without parentheses is not called unless explicitly qualified by this #20097

Closed
sjbiaga opened this issue Apr 5, 2024 · 3 comments
Labels
itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label

Comments

@sjbiaga
Copy link

sjbiaga commented Apr 5, 2024

Compiler version

3.4.2-RC1

Minimized code

object Test:
  import C.f
  class C:
    def m(): Unit = println("m()")
    def test: Unit = for { i <- 1 to 2 } do { this.f; println(i); f }
 object C:
     extension(self: C) { def f: Unit = self.m() }

Output

m()
1
m()
2

Expectation

The second (or any) unqualified call to f should use C.this for self.

m()
1
m()
m()
2
m()

Nevertheless, if the first this.f is replaced with unqualified call to f, the compiler issues a warning:

missing argument list for value of type Test.C => Unit
@sjbiaga sjbiaga added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Apr 5, 2024
@odersky
Copy link
Contributor

odersky commented Apr 5, 2024

Yes, that's as specced. Extension methods require an explicit receiver.

@sjbiaga
Copy link
Author

sjbiaga commented Apr 5, 2024

Thanks,
I wasn't decided between using empty parentheses (which is unnecessary) or qualifying with this (which is ugly).

@sjbiaga sjbiaga closed this as completed Apr 5, 2024
@som-snytt som-snytt closed this as not planned Won't fix, can't repro, duplicate, stale Apr 5, 2024
@som-snytt
Copy link
Contributor

There was an old scala 2 ticket for the same intuition for implicits (by paulp).

I agree that it's too bad there is no warning about the mistake. The error in "statement position" is very helpful.

I had to remind myself why -Wnonunit-statement is also unhelpful. (The warning is ignored by IterableOnce et al.) As a mere lint, it could special-case foreach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label
Projects
None yet
Development

No branches or pull requests

3 participants