Skip to content

Extension methods on different type with same name clash, when using default arguments #15140

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

Closed
crater2150 opened this issue May 8, 2022 · 4 comments
Labels
itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label

Comments

@crater2150
Copy link

Compiler version

3.1.2

Minimized code

extension (i: Int)
  def foo(x:String = "") = ???
extension (d: Double)
  def foo(x:String = "") = ???

Output

% scalac -3.1.2 -Xprint:typer Main.scala
-- Error: Main.scala:4:6 -------------------------------------------------------
4 |  def foo(x:String = "") = ???
  |      ^
  |      two or more overloaded variants of method foo have default arguments
[[syntax trees at end of                     typer]] // Main.scala
package <empty> {
  final lazy module val Main$package: Main$package = new Main$package()
  final module class Main$package() extends Object() { 
    this: Main$package.type =>
    extension (i: Int) def foo(x: String): Nothing = ???
    def foo$default$2(i: Int): String = ""
    extension (d: Double) def foo(x: String): Nothing = ???
    def foo$default$2(d: Double): String = ""
  }
}

1 error found

Expectation

This should compile, as the methods aren't on the same type, so they should not be ambiguous. Similar to #12245, also @targetName has no effect on the error

@crater2150 crater2150 added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels May 8, 2022
@pikinier20
Copy link
Contributor

On the one hand, the ambiguity of these two methods comes from the way the compiler represents extension methods:

extension (i: Int)
  def foo(x:String = "")

is under the hood

def foo(i: Int)(x: String)

On the other hand, the not working targetName annotation might be a bug.

@smarter Could you shed some light on this?

@nicolasstucki
Copy link
Contributor

The best solution for this is to wait until we improve how we handle default arguments. We plan to inline them, which would imply we would not need the foo$default$2 at runtime. We still have some hurdles to overcome to get there.

@smarter
Copy link
Member

smarter commented May 9, 2022

@targetName doesn't change the fact that methods are overloaded from the perspective of the Scala typechecker so it won't help here.

@smarter smarter removed their assignment May 9, 2022
@pikinier20
Copy link
Contributor

So basically, this limitation isn't a bug and comes from the compiler design and its representation of extension methods and default arguments. The @targetName annotation doesn't affect a typechecker where the error is thrown.

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

4 participants