Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin when with multiple values not working when value is an android view

Tags:

android

kotlin

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?

like image 999
jonathanrz Avatar asked May 20 '17 13:05

jonathanrz


People also ask

Can kotlin function return multiple values?

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.

Can when statement in kotlin be used without passing any argument?

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.

How do I return a triple in Kotlin?

Functions. toString(): This function returns the string equivalent of the Triple. Kotlin program of using the function: Kotlin.

When multiple is 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.


1 Answers

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")         }     } } 
like image 61
Daniel Storm Avatar answered Sep 20 '22 23:09

Daniel Storm