Kaita John
Kaita John

Reputation: 1109

How to check data type in Kotlin

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 {

 }

Upvotes: 1

Views: 643

Answers (1)

Joffrey
Joffrey

Reputation: 37680

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.

Upvotes: 3

Related Questions