https://kotlinlang.org logo
#announcements
Title
# announcements
i

irus

08/06/2017, 8:09 AM
Copy code
fun main(args: Array<String>) {
    val s = ""
    val i = 0
    val b = false
    
    println(s::class == String::class)
    println(i::class == Int::class)
    println(b::class == Boolean::class)

    fun test(a: Any) = when (a::class) {
        String::class -> println("String")
        Int::class -> println("Int")
        Boolean::class -> println("Boolean")
        else -> println("Else")
    }

    test(s)
    test(i)
    test(b)
}
What you expecting here?
answer in thread
absolutely not expected for me:
Copy code
true
true
true
String
Else
Else
And fix:
Copy code
fun test(a: Any) = when (a::class) {
    String::class -> println("String")
    Int::class, Int::class.javaPrimitiveType -> println("Int")
    Boolean::class, Boolean::class.javaPrimitiveType -> println("Boolean")
    else -> println("Else")
}
OMG! Platform depended code
k

karelpeeters

08/06/2017, 8:11 AM
That's strange indeed.
d

Dmitry Kandalov

08/06/2017, 1:14 PM
Haha, great puzzler 👍
i

ilya.gorbunov

08/06/2017, 1:28 PM
This seems to be some optimization gone wrong. Please report an issue.
Oh, it may be already reported as https://youtrack.jetbrains.com/issue/KT-17879
2 Views