df
11/03/2020, 2:48 PMval foo = doSomething();
if (!foo) return;
and
val foo = doSomething() ?? return
would be nice.Ruckus
11/03/2020, 2:55 PMtrue
after that, so there's no need for foo
. Just say if (!doSomething()) return
.df
11/03/2020, 2:56 PMval items = repo.getPending()
if (items.isEmpty()) {
return
}
items.forEach { ... }
Ruckus
11/03/2020, 2:56 PMdf
11/03/2020, 2:57 PMdf
11/03/2020, 2:57 PMRuckus
11/03/2020, 3:02 PMRuckus
11/03/2020, 3:05 PMrepo.getPending().takeIf { !it.isEmpty() } ?: return
But I personally think the if
check is more readableLuke
11/03/2020, 3:08 PMrepo.getPending().ifEmpty { return }
work?Tobias Berger
11/03/2020, 3:08 PM!it.isEmpty()
with it.isNotEmpty()
)
I personally prefer this to to the if
check, but I guess that's just a style decision. Anyway, it combines a condition with the elvis operatorRuckus
11/03/2020, 3:10 PMTobias Berger
11/03/2020, 3:11 PMtakeIf
version can still be useful if you're checking a condition on something else.Nir
11/03/2020, 3:16 PMNir
11/03/2020, 3:16 PMNir
11/03/2020, 3:17 PMTobias Berger
11/03/2020, 3:18 PMdf
11/03/2020, 3:23 PMdf
11/03/2020, 3:24 PMNir
11/03/2020, 3:38 PMdoSomething
Nir
11/03/2020, 3:38 PMdoSomething
if doSomething itself decides to throwRuckus
11/03/2020, 3:46 PMforEach
) as it won't do any iterations on an empty list.df
11/03/2020, 3:55 PMRuckus
11/03/2020, 3:56 PMdf
11/03/2020, 3:56 PMxxfast
11/03/2020, 4:00 PM?
Stands for. In kotlin, question mark is always and exclusively for nullability notationRob Elliot
11/03/2020, 4:03 PMdf
11/03/2020, 4:09 PM