https://kotlinlang.org logo
Title
n

Nicolas Chaduc

05/25/2018, 8:18 AM
what do you think about two extensions :
fun ifLet(value: Any?, code: ((Any) -> Unit)): Boolean {
    value?.let {
        code.invoke(it)
        return true
    }
    return false
}

fun Boolean.elseLet(code: () -> Unit) {
    if (!this) {
        code.invoke()
    }
}
for use :
ifLet(myMutableValue, {
            System.out.println("Call of ifLet Code myInt value = $it")
        }).elseLet {
            System.out.println("Call of elseLet Code")
        }
?
d

diesieben07

05/25/2018, 8:22 AM
Honestly, not much. I would prefer the expressiveness of an explicit local
val
and a normal if.
1