Skip to content

Instantly share code, notes, and snippets.

@kmizu
Last active September 2, 2016 13:44
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/8bd8f1b3b4a97bd2b100f3dc621ecbe0 to your computer and use it in GitHub Desktop.
Save kmizu/8bd8f1b3b4a97bd2b100f3dc621ecbe0 to your computer and use it in GitHub Desktop.
Type unsoundness of Kotlin
$ kotlinc Main.kt
$ kotlin MainKt
Exception in thread "main" java.lang.ClassCastException: java.lang.Object cannot be cast to B
at B.compareTo(Main.kt:4)
at MainKt.main(Main.kt:9)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.jetbrains.kotlin.runner.AbstractRunner.run(runners.kt:60)
at org.jetbrains.kotlin.runner.Main.run(Main.kt:99)
at org.jetbrains.kotlin.runner.Main.main(Main.kt:104)
open class A() {
open fun compareTo(o: Any): Int = 0
}
class B() : A(), Comparable<B> {
override fun compareTo(o: B): Int = 0
}
fun main(args: Array<String>) {
println(B().compareTo(Object()))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment