Why does this work: ```data class TestContainer( ...
# announcements
a
Why does this work:
Copy code
data class TestContainer(
	val body: Any?
)

operator fun TestContainer?.compareTo(b: TestContainer?): Int = this?.body.let { aBody ->
    b?.body.let { bBody ->
        when {
            aBody is Boolean && bBody is Boolean -> {
                val aBoolean: Boolean = aBody
                val bBoolean: Boolean = bBody
                return aBoolean.compareTo(bBoolean)
            }
            else -> -1
        }
    }
}

fun main() {
	println("Comparison: ${TestContainer(true) < TestContainer(false)}")
}
But the following yields ClassCastException at runtime on JVM?
Copy code
data class TestContainer(
	val body: Any?
)

operator fun TestContainer?.compareTo(b: TestContainer?): Int = this?.body.let { aBody ->
    b?.body.let { bBody ->
        when {
            aBody is Boolean && bBody is Boolean -> aBody.compareTo(bBody)
            else -> -1
        }
    }
}

fun main() {
	println("Comparison: ${TestContainer(true) < TestContainer(false)}")
}
Error is
Copy code
Exception in thread "main" java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.Number
I'd expect the two to be equivalent due to smart cast?
t
I'd say this is definitely a bug. If you manually cast the values, the IDE will even tell you that the cast is unnecessary. However, when you look at the decompiled jvm bytecode you see that instead of
(Boolean)aBody
you get
((Number)aBody).intValue()
a
@Tobias Berger Thanks for checking. I created an issue here: https://youtrack.jetbrains.com/issue/KT-46467