I implemented a function that is used in anko's apply recursively:
fun applyTemplateViewStyles(view: View) { when(view) { is EditText, TextView -> { .... } } }
And I receive an error saying that "Function invocation 'TextView(...)' expected"
Since I can write an when with a clause like is 0, 1, why I can't do the same with an Android View?
In this article, we talked about the fact that Kotlin doesn't support tuples, so we can't return multiple values from functions using tuples. Despite this bad news, we can still use destructuring declarations to mimic the tuple behavior for returning multiple values from a function.
Using when Without an Argument The last interesting feature of when is that it can be used without an argument. In such case it acts as a nicer if-else chain: the conditions are Boolean expressions.
Functions. toString(): This function returns the string equivalent of the Triple. Kotlin program of using the function: Kotlin.
In kotlin, we can use when multiple conditions by using two ways: when as a statement and when as an expression. Multiple conditions in kotlin will take the value as an input and check the values against the condition we have provided when multiple conditions are important in kotlin.
You're missing the other is
:
fun applyTemplateViewStyles(view: View) { when(view) { is EditText, is TextView -> { println("view is either EditText or TextView") } else -> { println("view is something else") } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With