Home > Software engineering >  How to check data type in Kotlin
How to check data type in Kotlin

Time:02-28

Am checking whether value returned by a function is a snapshot in Kotlin as indicated below, however I would like to check for the exception first(ie when it is not a snapshot). I have tried using !=, equal(), but they all compare but can't check datatype. How can I get the else part first?

 if (it is Snapshot) {

 } else {

 }

CodePudding user response:

You can negate the is operator with !is:

if (it !is Snapshot) {
   // not a Snapshot
} else {

}

The doc for this is in the hard keywords section.

  • Related