December 2017
Intermediate to advanced
260 pages
7h 34m
English
Typically, when replaces the switch case of traditional programming languages such as C, Java, and so on. A simple when block can look as follows:
when (x) { 1 -> print("x == 1") 2 -> print("x == 2") else -> { // Note the block print("x is neither 1 nor 2") } }
No verbosity and the simplest to understand isn't it! We have used the when block in the app at multiple places; in the chapter, we will quote one from the DelegatedPrefernce.kt file. The when() function can be used as an expression or as a statement. The following is the code we have used in our app:
val result: Any = when (defaultValue) { is Boolean -> getBoolean(key, defaultValue) is Int -> getInt(key, defaultValue) is Long -> getLong(key, defaultValue ...
Read now
Unlock full access