Skip to content

Overloading doesn't work with function types #7792

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
anatoliykmetyuk opened this issue Dec 17, 2019 · 5 comments
Closed

Overloading doesn't work with function types #7792

anatoliykmetyuk opened this issue Dec 17, 2019 · 5 comments

Comments

@anatoliykmetyuk
Copy link
Contributor

anatoliykmetyuk commented Dec 17, 2019

minimized code

trait Foo {
  def g(f: Int => Int): Int = 1
  def g(f: String => String): String = "2"
}

@main def Test =
  val m: Foo = new Foo {}
  m.g(a => 1)
-- [E051] Reference Error: /Users/kmetiuk/Projects/scala3/pg/sandbox/iss4.scala:8:4
8 |  m.g(a => 1)
  |  ^^^
  |Ambiguous overload. The overloaded alternatives of method g in trait Foo with types
  | (f: String => String): String
  | (f: Int => Int): Int
  |both match arguments (? => ?)

It works in Scala 2. If you explicitly specify the type of the lambda argument, m.g((x: Int) => 1), it also works.

expectation

The code should correctly resolve the overload alternative.

@anatoliykmetyuk anatoliykmetyuk changed the title Overloading doesn't work with type aliases of function types Overloading doesn't work with function types Dec 17, 2019
@anatoliykmetyuk
Copy link
Contributor Author

Not sure if that's a real issue since we are working with untyped trees of arguments on overload resolution time, so we may not have a way to precisely resolve the type of ? => ?...

@anatoliykmetyuk
Copy link
Contributor Author

Though it works with

trait Foo {
  def g(f: Int => Int): Int = 1
  def g(f: Int => String): String = "2"
}

(Second g's lambda accepts an Int) for some reason. Hence should also work in the original case.

@odersky
Copy link
Contributor

odersky commented Dec 17, 2019

I think that's precisely where Scala 2 and Dotty differ when dealing with overloaded functions taking lambda arguments. The Dotty version is simpler but also a bit more restrictive. We need to decide what the right spec is.

@sjrd
Copy link
Member

sjrd commented Dec 17, 2019

Handling this kind of situation was added in Scala 2.13, because the new Scala collections basically rely on this working. They use overloading much more, whereas previously things were handled with complicated CanBuildFrom instances.

@odersky
Copy link
Contributor

odersky commented Apr 5, 2022

I don't think we will change this, after all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants