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
Unsoundness involving generic type and same-named methods #9912
Comments
Imported From: https://issues.scala-lang.org/browse/SI-9912?orig=1 |
@szeiger said: {quote}It is also an error if a template contains two members (directly defined or inherited) with the same name and the same erased type.{quote} While compareTo in B has a different erasure than compareTo in A, B also inherits (and overrides) compareTo from Comparable, which has the same erasure. |
kenji yoshida said: Welcome to Scala version 2.9.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_79).
Type in expressions to have them evaluated.
Type :help for more information.
scala> :paste
// Entering paste mode (ctrl-D to finish)
class A {
def compareTo(o: Any): Int = 0
}
class B extends A with Comparable[B] {
def compareTo(b: B): Int = 0
}
object C {
def main(args: Array[String]): Unit = {
println(new B().compareTo(new Object()))
}
}
// Exiting paste mode, now interpreting.
<console>:10: error: name clash between inherited members:
method compareTo:(o: Any)Int in class A and
method compareTo:(x$1: B)Int in trait Comparable
have same type after erasure: (o: java.lang.Object)Int
class B extends A with Comparable[B] {
^ Welcome to Scala version 2.10.6 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_102).
Type in expressions to have them evaluated.
Type :help for more information.
scala> :paste
// Entering paste mode (ctrl-D to finish)
class A {
def compareTo(o: Any): Int = 0
}
class B extends A with Comparable[B] {
def compareTo(b: B): Int = 0
}
object C {
def main(args: Array[String]): Unit = {
println(new B().compareTo(new Object()))
}
}
// Exiting paste mode, now interpreting.
<console>:10: error: name clash between inherited members:
method compareTo:(o: Any)Int in class A and
method compareTo:(x$1: B)Int in trait Comparable
have same type after erasure: (o: Object)Int
class B extends A with Comparable[B] {
^ |
@adriaanm said: |
The following code
compiles without warnings. And it should be executed without any runtime error. However, ClassCastException occurs when the code is executed.
This problem is also known as an already fixed bug of Java Generics:
http://www.concurrentaffair.org/2006/10/10/java-generics-unsound/
The text was updated successfully, but these errors were encountered: