Pablo
06/15/2021, 10:42 AMfind
or firstOfNull
?
val foo = bar.firstOfNull {whatever}
if(foo==null) smthing()
else smthing2()
Emil Kantis
06/15/2021, 10:47 AMbar.find { whatever}?.let { smthng(it) } ?: smthng2()
Vampire
06/15/2021, 11:06 AMlet
returns the result of the lambda.
So if smthng
returns null
, both methods would be executed.
Better would be
bar.firstOrNull { whatever }?.also { smthng() } ?: smthng2()
Vampire
06/15/2021, 11:07 AMsmthing()
can never return null
, then it shouldn't make a differenceJavier
06/15/2021, 11:33 AMVampire
06/15/2021, 11:33 AMJoost Klitsie
06/15/2021, 12:41 PMif (bar.any { whatever }) {
smthing()
}
else {
smthing2()
}
Dominaezzz
06/15/2021, 1:15 PMJoost Klitsie
06/15/2021, 1:16 PMNir
06/15/2021, 1:31 PMbar.firstOrNull { whatever }.run {
if (it == null) {
something()
else {
something2(it)
}
}