https://kotlinlang.org logo
Title
y

Yossi Saiada

05/01/2019, 11:50 AM
Is there any simpler way to write it:
if (y) x.getSomething() else null
Note that
x.getSomething().takeIf { y }
isn't identical, because it always calculate
x.getSomething()
.
🇳🇴 2
t

thana

05/01/2019, 12:11 PM
note: just because its /shorter/ that doesnt mean it's better or simpler or easier to understand. i think there is absolutly nothing wrong with writing the full if/else even if there were a shorter version
☝️ 2
w

wbertan

05/01/2019, 12:13 PM
x.takeIf { y }?.getSomething()
🤣 But agree with @thana 👍
s

Stephan Schroeder

05/01/2019, 12:13 PM
is this simpler??
x::getSomething.takeIf{y}?.let{it()}
damn it, William’s is simpler 😆
k

karelpeeters

05/01/2019, 12:32 PM
William's is technically not equivalent, it evaluates
x
twice.