Thread
#stdlib
    alexfacciorusso

    alexfacciorusso

    3 years ago
    Has the introduction of something like:
    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

    3 years ago
    I imagine you have a use case in mind? I’m not sure why this would be preferable to a standard if/else
    alexfacciorusso

    alexfacciorusso

    3 years ago
    Example (something I’ve just done):
    fun setLoading(loading: Boolean) = loading.map(View.VISIBLE, View.INVISIBLE)
    I mean, pardon, the full implementation is:
    override fun showLoading(loading: Boolean) {
        progressBar.visibility = loading.map(View.VISIBLE, View.INVISIBLE)
    }
    Shawn

    Shawn

    3 years ago
    progressBar.visibility = if (loading) View.VISIBLE else View.INVISIBLE
    much clearer than
    .map(??, ??)
    s

    serebit

    3 years ago
    I can definitely see the use case for it, although I do think the if/else is more readable
    j

    jw

    3 years ago
    if/else is also lazy
    if (loading) computeSomething() else computeOtherThing()