Question about "is"


#1

This code:

/**
* Created on 2/18/14.
*/

fun main(args: Array<String>)
{
  val temp = “abc”
  if (temp is Int)
  println(“variable is Int”)
  else
  println(“variable is not Int”)
}

is giving this error:

Error:(8, 17) Kotlin: Incompatible types: jet.Int and jet.String

Is this an incorrect usage of “is” or am I missing something obvious?


#2

Compiler detects that this condition is always false (this is easy since String and Int are final classes)


#3

Got it. Thanks!

With a slightly different error message It might be a little clearer to amateurs on why it was creating an error: “Incompatible types: condition will always be false”.

Although maybe the error is not with regard to “condition is always false” because I see that there is no error for this code:

  if ( 2 > 3)
  println(“greater”)
  else
  println(“less than”)


#4

Fair point. I've added issue for it: KT-4597. It's better to post bug reports and feature/improvement suggestions directly there.