October 2019
Intermediate to advanced
434 pages
11h 54m
English
We can check whether a variable is of a particular type by using the is keyword. For example, we could check whether settingsProvider is an instance of SettingsManager as follows:
fun main(args: Array<String>) { val settingsProvider: SettingsProvider = SettingsManager if (settingsProvider is SettingsManager) { // do something }}
Conversely, you can check whether a variable is not of a certain type by using !is:
fun main(args: Array<String>) { val settingsProvider: SettingsProvider = SettingsManager if (settingsProvider !is SettingsManager) { // do something }}
The Kotlin compiler can track explicit casts and type checks and can perform smart casting for us. This means that, once the compiler has validated a variable's type, ...
Read now
Unlock full access