https://kotlinlang.org logo
#announcements
Title
# announcements
t

thana

07/11/2019, 8:28 AM
what do you think about
Copy code
infix fun <T : Any?> T.`if`(condition: Boolean): Pair<Boolean, T> =
    Pair(condition, this)

infix fun <T> Pair<Boolean, T>.`else`(block: () -> T) =
    if (component1()) component2()
    else block()
p

Pavlo Liapota

07/11/2019, 8:32 AM
Do you want to achieve something like that?
Copy code
value.takeIf { it >= 0 } ?: defaultValue
t

thana

07/11/2019, 8:34 AM
no i thought about a slightly more readable version of
return if (condition) foo else bar
and put the
foo
in front of
if
so with the two methods i could write
Copy code
return foo `if` (condition) `else` bar
the really annoying part are the backticks of course 😉
p

Pavlo Liapota

07/11/2019, 8:37 AM
Why do you think it is more readable than
return if (condition) foo else bar
?
t

thana

07/11/2019, 8:38 AM
because it's close to human language
2
🤷‍♂️ 1
k

karelpeeters

07/11/2019, 10:40 AM
I've always found this to be annoying in python, the condition is between the values making it hard to compare them.
2 Views