Kotlin for Android Developers
Download 1.04 Mb. Pdf ko'rish
|
Kotlin for Android Developers Learn Kotlin the Easy Way While Developing an Android App ( PDFDrive )
- Bu sahifa navigatsiya:
- 22.1 If Expression
- 22.2 When expression
22. Flow control and ranges
I’ve been using some conditional expressions in our code, but now it’s time to explain them more deeply. Though we’ll usually use less mechanisms to control the flow of the code that we’d normally use in a completely procedural programming language (some of them even practically disappear), they are still useful. There are also new powerful ideas that will solve some particular problems much easier. 22.1 If Expression Almost everything in Kotlin is an expression, which means it returns a value. If conditions are not an exception, so though we can use if as we are used to do: 1 if (x > 0) { 2 toast("x is greater than 0") 3 } else if (x == 0) { 4 toast("x equals 0") 5 } else { 6 toast("x is smaller than 0") 7 } We can also assign its result to a variable. We’ve used it like that several times in our code: 1 val res = if (x != null && x.size() >= days) x else null This also implies we don’t need a ternary operation similar to the Java one, because we can solve it easily with: 1 val z = if (condition) x else y So the if expression always returns a value. If one of the branches returns Unit , the whole expression will return Unit , which can be ignored, and it will work as a regular Java if condition in that case. 98 22. Flow control and ranges 99 22.2 When expression When expressions are similar to switch/case in Java, but far more powerful. This expression will try to match its argument against all possible branches in order until it finds one that is satisfied. It will then apply the right side of the expression. The difference with a switch/case in Java is that the argument can be literally anything, and the conditions for the branches too. For the default option, we can add an else branch that will be executed if none of the previous conditions is satisfied. The code executed when a condition is satisfied can be a block too: 1 when (x) { 2 1 -> print("x == 1") 3 2 -> print("x == 2") 4 else -> { 5 print("I'm a block") 6 print("x is neither 1 nor 2") 7 } 8 } As it’s an expression, it can return a result too. Take into consideration that when used as an expression, it must cover all the possible cases or implement the else branch. It won’t compile otherwise: 1 val result = when (x) { 2 0, 1 -> "binary" 3 else -> "error" 4 } As you can see, the condition can be a set of values separated with commas. But it can be many more things. We could, for instance, check the type of the argument and take decisions based on this: 1 when(view) { 2 is TextView -> view.setText("I'm a TextView") 3 is EditText -> toast("EditText value: ${view.getText()}") 4 is ViewGroup -> toast("Number of children: ${view.getChildCount()} ") 5 else -> view.visibility = View.GONE 6 } The argument is automatically casted in the right part of the condition, so you don’t need to do any explicit casting. It’s possible to check whether the argument is inside a range (I’ll explain ranges later in this chapter), or even inside a collection: 22. Flow control and ranges 100 1 val cost = when(x) { 2 in 1..10 -> "cheap" 3 in 10..100 -> "regular" 4 in 100..1000 -> "expensive" 5 in specialValues -> "special value!" 6 else -> "not rated" 7 } Or you could even get rid of the argument and do any crazy check you may need. It could easily substitute an if / else chain: 1 val res = when { 2 x in 1..10 -> "cheap" 3 s.contains("hello") -> "it's a welcome!" 4 v is ViewGroup -> "child count: ${v.getChildCount()}" 5 else -> "" 6 } Download 1.04 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling