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

inference fails on overloaded methods despite non-conflicting signature #10542

Open
cleishm opened this issue Oct 9, 2017 · 3 comments
Open
Labels
fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) overloading
Milestone

Comments

@cleishm
Copy link

cleishm commented Oct 9, 2017

Asked on StackOverflow: https://stackoverflow.com/questions/46652058/scala-type-inference-fails-on-overloaded-methods-despite-non-conflicting-signatu

% scala                                                                     
Welcome to Scala 2.12.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_111).
Type in expressions for evaluation. Or try :help.

scala>   trait Op[-Y, -Z, +A, +B] {
     |     def apply(other: (Y, Z)): (A, B)
     |   }
defined trait Op

scala>   implicit class RichTuple2[+A, +B](t: (A, B)) {
     |     def ~~~(other: Int): (A, B) = ???
     |     def ~~~[RA, RB](other: Op[A, B, RA, RB]): (RA, RB) = other.apply(t)
     |   }
defined class RichTuple2

scala>   def swap[A, B] = new Op[A, B, B, A] {
     |     override def apply(other: (A, B)) = (other._2, other._1)
     |   }
swap: [A, B]=> Op[A,B,B,A]

scala> (1, "foo") ~~~ swap
<console>:14: error: overloaded method value ~~~ with alternatives:
  [RA, RB](other: Op[Int,String,RA,RB])(RA, RB) <and>
  (other: Int)(Int, String)
 cannot be applied to (Op[Nothing,Nothing,Nothing,Nothing])
       (1, "foo") ~~~ swap

Removing the unused and non-conflicting method def ~~~(other: Int) results in successful inference and compilation.

Possibly related to #9523

@Jasper-M
Copy link
Member

Jasper-M commented Oct 9, 2017

Actually you don't have to try so hard to get to see this beautiful error.

scala> object Foo {
     |   def foo(op: Op[Int,String,String,Int]) = op
     |   def foo(i: Int) = i
     | }
defined object Foo

scala> Foo.foo(swap)
<console>:15: error: overloaded method value foo with alternatives:
  (i: Int)Int <and>
  (op: Op[Int,String,String,Int])Op[Int,String,String,Int]
 cannot be applied to (Op[Nothing,Nothing,Nothing,Nothing])
       Foo.foo(swap)
           ^

This has always been a feature of overloaded methods.

The explanation probably contains terms like "expected type".

@cleishm
Copy link
Author

cleishm commented Oct 11, 2017

@Jasper-M: you're right, that's a much easier example :)

@SethTisue SethTisue added this to the Backlog milestone Oct 12, 2020
@SethTisue SethTisue added the fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) label Jan 12, 2024
@SethTisue
Copy link
Member

works in Scala 3.3.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) overloading
Projects
None yet
Development

No branches or pull requests

3 participants