-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Overload in Scala 2 treated as Override in Dotty #4781
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
Comments
Pattern used in the 2.13 standard library: https://github.com/scala/scala/blob/6dfcda8e98c371d3292c2f7b65277b1b4c48ece6/src/library/scala/collection/mutable/AnyRefMap.scala#L429-L438 |
I wonder if there's any sane way to support this in Dotty, even for Scala2 mode. |
scala> def foo[T](x: T & AnyRef) = 1
def foo[T](x: T & AnyRef): Int
scala> foo[String]("Hello")
val res2: Int = 1
scala> foo[Int](1)
1 |foo[Int](1)
| ^
| found: Int(1)
| required: Int & AnyRef
| |
But if
and they also can't be overloads because they erase to the same thing. Since Scala3
Still, it's true that sometimes the compiler keeps the distinction, but I don't think that's reliable (it's just like dealiasing). The corrected version of your example (with
|
@adriaanm confirmed this was a bug in scalac. @odersky suggested to use an extra dummy parameter if a function needs to be an overload: class Map[T] { def foo(x: T): Int = 1 }
class AnyRefMap[T <: AnyRef] extends Map[T] {
class Dummy
def foo(y: T)(implicit $dummy: Dummy = null): Int = 2
} scala> (new AnyRefMap[String]).foo("Hello")
val res1: Int = 2
scala> (new AnyRefMap[String]: Map[String]).foo("Hello")
val res2: Int = 1 Note that I could only make it work with an |
I'm pretty sure our implementation of |
@adriaanm would be interesting to try the naive correct implementation in Scala 2 and see if it has any performance impact. |
It would be interesting, yet non-trivial. Could shake out some more bugs too (i.e., consider me nerd-sniped ;-)) |
@adriaanm FWIW I thought Scala 2's
EDIT: in particular, that means that the |
I've created #11035 to ensure that Scalac fixes this; this should be handled by 2.14 at the latest. Makes sense @adriaanm? @allanrenucci is fixing the stdlib, so this will be closed by #4825. |
The text was updated successfully, but these errors were encountered: