class Foo(val l: Long) : Comparable { override fun compareTo(other: Foo): Int { return l.compareTo(other.l) } } class Bar(val l: Int) : Comparable { override fun compareTo(other: Bar): Int { return l.compareTo(other.l) } } operator fun TLhs.compareTo(rhs: TRhs): Int = (this as Comparable).compareTo(rhs as TLhs) infix fun Any?.isGreaterThan(expected: Any?): Boolean { if (this == null) return false if (expected == null) return false return this > expected } fun main(args: Array) { val aFoo = Foo(1L) val aBar = Bar(1) println(aFoo isGreaterThan aBar) } // On JVM: // Exception in thread "main" java.lang.ClassCastException: Bar cannot be cast to Foo // at Foo.compareTo(main.kt:1) // at MainKt.compareTo(main.kt:13) // at MainKt.isGreaterThan(main.kt:18) // at MainKt.main(main.kt:24) // 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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) // On JS // Uncaught TypeError: other.isNegative is not a function kotlin.js:2258 // which is var otherNeg = other.isNegative(); in Kotlin.Long.compareTo