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

Method call fails for overloaded methods with default parameters #12679

Closed
danicheg opened this issue Jun 2, 2021 · 4 comments · Fixed by #17502
Closed

Method call fails for overloaded methods with default parameters #12679

danicheg opened this issue Jun 2, 2021 · 4 comments · Fixed by #17502
Labels
backlog No work planned on this by the core team for the time being. itype:bug

Comments

@danicheg
Copy link
Contributor

danicheg commented Jun 2, 2021

Compiler version

3.0.0

Minimized code

object Foo extends App {
  def foo[F[_]](qux: String, quux: String = ""): F[Unit] = ???

  def foo[F[_]](qux: Boolean): F[Unit] = ???

  def example[F[_]](maybeQux: Option[String], bool: Boolean) =
    maybeQux.fold(foo[F](bool))(foo[F](_))
}

https://scastie.scala-lang.org/LnY1kBvlQs2zclBzaeA0Vg

Output

7 |    maybeQux.fold(foo[F](bool))(foo[F](_))
  |                                ^^^
  |None of the overloaded alternatives of method foo in object Foo with types
  | [F[_$2]](qux: Boolean): F[Unit]
  | [F[_$1]](qux: String, quux: String): F[Unit]
  |match type arguments [F] and arguments ((_$4 : String))

Expectation

That works in Scala 2 (both 2.12.13 and 2.13.6).

@danicheg
Copy link
Contributor Author

danicheg commented Jun 2, 2021

The same example without higher kinded type works well:

object Foo extends App {
  def foo(qux: String, quux: String = ""): Unit = ()

  def foo(qux: Boolean): Unit = ()

  def example(maybeQux: Option[String], bool: Boolean) =
    maybeQux.fold(foo(bool))(foo(_))
}

https://scastie.scala-lang.org/b9jmQKk4STKND80gRy6cbQ

@bishabosha
Copy link
Member

I have updated your example to remove the dependency on cats - it still fails with the same error

@danicheg
Copy link
Contributor Author

It's also reproduce on 3.0.1-RC1: https://scastie.scala-lang.org/b03p2cqfQiirhjQKLsdKFw

@odersky
Copy link
Contributor

odersky commented Oct 4, 2021

I tried some things, but have to give up fow now. The combination of overloading and default parameters is so complex that any change is extremely difficult. I think the right move is to go back to the drawing board and change the design of default parameters. Instead of the super-fragile default getter name encoding we should attach an inline expression directly to the parameter. That would improve and expressiveness, performance and decrease complexity. But it's an incompatible language change since default arguments could not be overridden anymore.

So, unless someone else feels more courageous than myself, don't hold your breath for a fix. It's better to avoid the combination of overloading and default parameters, except if the default parameter appears in the first parameter clause of the method.

@odersky odersky added the backlog No work planned on this by the core team for the time being. label Apr 7, 2022
ckipp01 added a commit to ckipp01/dotty that referenced this issue May 14, 2023
ckipp01 added a commit to ckipp01/dotty that referenced this issue May 14, 2023
nicolasstucki added a commit that referenced this issue May 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backlog No work planned on this by the core team for the time being. itype:bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants