sindrenm
12/08/2020, 10:17 PMoperator fun <T> State<T>.component1(): T = value
? Being able to destructure a State
is very useful when the value of the State
is nullable. Such as:
val (content) = viewModel.content.collectAsState()
if (content != null) {
SomeContent(content)
} else {
MissingContent()
}
(Using a StateFlow
here, but applies for any other way of retrieving a State
as well.)
I don't find the property delegate very useful in this situation, as smart-casting can't be done in that case (custom getter and all that). Is this something that would make sense to even add to the standard lib?Zach Klippenstein (he/him) [MOD]
12/08/2020, 10:23 PMval content = viewModel.content.collectAsState().value
Much less magical/clever and makes the code very readable if you don’t remember what the destructuring is forsindrenm
12/08/2020, 10:29 PMZach Klippenstein (he/him) [MOD]
12/08/2020, 10:55 PMsindrenm
12/08/2020, 11:34 PMval (content, setContent) = viewModel.content.collectAsState()
Where component2()
would return a setter for the value.Zach Klippenstein (he/him) [MOD]
12/09/2020, 12:54 AMMutableState
supports that exact syntax. It makes a bit more sense to me there since there are two values (the problem destructuring was designed to solve), but i will admit i’m still not a huge fan of it since i think it’s more readable and not much more boilerplate to write { content = foo }
when you need to update it.Adam Powell
12/09/2020, 2:26 AMSean McQuillan [G]
12/09/2020, 6:19 AM