Sam
02/12/2022, 10:47 PM@Composable
fun Greeting(name: String?) {
name?.let { Text(text = "Hello $name!") } ?: Text(text = "Hello!")
}
Rick Regan
02/13/2022, 1:51 AMAlexander Maryanovsky
02/13/2022, 5:41 AMnull
the else part of the ?: will be executed.Chrimaeon
02/13/2022, 11:46 AMAlexander Maryanovsky
02/13/2022, 12:06 PMname?.let { Text(text = "Hello $name!") }
if name
is null
?
On the face of it, it’s null
, but actually it’s Unit
. Not sure why, but there’s probably a good explanation.Text(…)
with println(…)
it works as expected, and the result is null
.Chrimaeon
02/13/2022, 12:11 PMname
and not the result of let
Alexander Maryanovsky
02/13/2022, 12:11 PMUnit
Chrimaeon
02/13/2022, 12:12 PMAlexander Maryanovsky
02/13/2022, 12:30 PMMichael Paus
02/13/2022, 1:40 PM@Composable
fun Greeting(name: String?) {
Text(if (name != null) "Hello $name!" else "Hello!")
}
not be several times more readable and also less error prone (for the developer and the compiler)?Sam
02/13/2022, 2:21 PMAlexander Maryanovsky
02/13/2022, 2:24 PMif … else …
Sam
02/13/2022, 2:25 PMAlexander Maryanovsky
02/13/2022, 2:25 PMlet
not returning null
Sam
02/13/2022, 2:25 PM