Pattern matching example failed to compile


#1

Hi,

I am trying out the example under “Pattern matching operators (is and !is)” from http://confluence.jetbrains.net/display/Kotlin/Pattern+matching like this:

``

fun main(args: Array<String>) {
     test(99)
     test(“hello”)
     test(java.util.Date())
}
fun test(x : Any) {
     when (x) {
          is Int -> print(x)
          is List<Int> -> print(x.sum())
          !is Number -> print(“Not even a number”)
          else -> print(“can’t do anything”)
     }
}

However this will not compile:

``

ERROR: /Users/zemian/apps/kotlinc-0.4.177/Test.kt: (9, 6) Cannot check for instance of erased type: jet.List<jet.Int>
ERROR: /Users/zemian/apps/kotlinc-0.4.177/Test.kt: (9, 27) Unresolved reference: sum
exec() finished with COMPILATION_ERROR return code

Even if I replace “print(x.sum())” with simply “print(x)”, I still get the first error.

Am I missing something?

Zemian


#2

Same problem as here?

http://stackoverflow.com/questions/13154463/how-can-i-check-for-generic-type-in-kotlin


#3

Yes, it's the same problem. I fixed the example, it should have said "List<*> instead of List<Int>"