Skip to content

Instantly share code, notes, and snippets.

@kmizu
Created September 2, 2016 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmizu/f435c0c71ba68ed86404a321a62baa6a to your computer and use it in GitHub Desktop.
Save kmizu/f435c0c71ba68ed86404a321a62baa6a to your computer and use it in GitHub Desktop.
Another type unsoundness of Scala
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()))
}
}
> scalac C.scala
> scala C
java.lang.ClassCastException: java.lang.Object cannot be cast to B
at B.compareTo(C.scala:4)
at C$.main(C.scala:9)
at C.main(C.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
at java.lang.reflect.Method.invoke(Method.java:498)
at scala.reflect.internal.util.ScalaClassLoader$$anonfun$run$1.apply(Sca
at scala.reflect.internal.util.ScalaClassLoader$class.asContext(ScalaCla
at scala.reflect.internal.util.ScalaClassLoader$URLClassLoader.asContext
at scala.reflect.internal.util.ScalaClassLoader$class.run(ScalaClassLoad
at scala.reflect.internal.util.ScalaClassLoader$URLClassLoader.run(Scala
at scala.tools.nsc.CommonRunner$class.run(ObjectRunner.scala:22)
at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:39)
at scala.tools.nsc.CommonRunner$class.runAndCatch(ObjectRunner.scala:29)
at scala.tools.nsc.ObjectRunner$.runAndCatch(ObjectRunner.scala:39)
at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala
at scala.tools.nsc.MainGenericRunner.run$1(MainGenericRunner.scala:87)
at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:98)
at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:103)
at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment