https://kotlinlang.org logo
#stdlib
Title
# stdlib
a

alexfacciorusso

01/15/2019, 4:27 PM
Has the introduction of something like:
Copy code
fun <T> Boolean.map(valueTrue: T, valueFalse: T): T = if(this) valueTrue else valueFalse
in the stdlib been already asked/taken into consideration?
s

serebit

01/15/2019, 4:28 PM
I imagine you have a use case in mind? I’m not sure why this would be preferable to a standard if/else
👍🏼 1
👆 1
a

alexfacciorusso

01/15/2019, 4:31 PM
Example (something I’ve just done):
Copy code
fun setLoading(loading: Boolean) = loading.map(View.VISIBLE, View.INVISIBLE)
I mean, pardon, the full implementation is:
Copy code
override fun showLoading(loading: Boolean) {
    progressBar.visibility = loading.map(View.VISIBLE, View.INVISIBLE)
}
s

Shawn

01/15/2019, 4:35 PM
Copy code
progressBar.visibility = if (loading) View.VISIBLE else View.INVISIBLE
much clearer than
.map(??, ??)
1
s

serebit

01/15/2019, 4:36 PM
I can definitely see the use case for it, although I do think the if/else is more readable
j

jw

01/15/2019, 4:39 PM
if/else is also lazy
if (loading) computeSomething() else computeOtherThing()
☝️ 4