UndefinedBehavior
UndefinedBehavior

Reputation: 908

kotlin behavior with generics seems inconsistent

If I try to compile this code:

fun main(){
    val listOfStrings = listOf("hi", "hello")
    when(listOfStrings) {
        is List<Int> -> println("OK")   // Exception
    }

}

I get error: cannot check for instance of erased type 'kotlin.collections.List<kotlin.Int>' which seems reasonable because at runtime there is no List<Int> but simply List

If I compile this:

fun main(){
    val listOfStrings = listOf("hi", "hello")
    when(listOfStrings) {
        is List<String> -> println("OK")   // OK
    }
}

I get warning: check for instance is always 'true'.

My question is: why the different behaviour? If in the second case the compiler is smart enough to warn me that my condition always evaluates to true, why is it that in the first case it can't understand that the condition is always false? Or alternatively shouldn't it simply error out in both cases?

Upvotes: 4

Views: 44

Answers (1)

Dunes
Dunes

Reputation: 40903

The simple reason is that Kotlin allows you to do unchecked casts of generics. This would cause unintutive behaviour in your first example, and so the compiler must reject it.

Example of an unchecked cast:

val anys: List<Any> = listOf("abc")
// Warning: Unchecked cast: `List<Any>` to `List<String>`
val strings: List<String> = anys as List<String>

All that is checked here is that anys is an instance of List. This is potentially unsafe, as you could do the following:

anys.append(123)
val mystring = strings.get(1)  // ERROR

The append() breaks the contract of strings. That is, the compiler assumes strings.get(1) should return a String, but at runtime an Int would be returned.

This effects your first code sample, because although it looks like it should always be false, it would in fact always be true. The cast from List<String> to List<Int> would succeed, and the when branch entered. It would appear that while the unchecked cast is allowed, using it in a when block is prohibited.

In the second example, the compiler already knows the argument is the requested type, and so knows that the check will both succeed AND be safe. However, it also knows that the when block is pointless and so instead emits a warning.

Upvotes: 1

Related Questions