dimanche 29 décembre 2019

Why the Scala method isInstanceOf[T] is not working

Why the isInstanceOf[T] method is not working as intended?

In the following, I have defined a hello class and companion object. In the hello object, I test this.isInstanceOf[T] in the line of codes " hel.typetest[Int] ", how come this is true when the type T is Int?

object hello {
  def main(args: Array[String]): Unit = {
    Console.println("main")
    val hel = new hello
    hel.typetest[Int]
  }
}

class hello {
  def typetest[T: ClassTag]: Unit = {
    Console.println(this.isInstanceOf[T])
    Console.println(this.getClass)
  }
}

Output:

main
true
class hello




Aucun commentaire:

Enregistrer un commentaire