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

shiraji

06/19/2017, 11:11 PM
I would like to have a block after elvis
Copy code
val bar = foo ?: {
    println("foo is null!")
    return // return for block? return for what??? I don't know.
}
Is there any discussion about this? or no one interested having this feature? I know Kotlin can do this but still I want to have a block after elvis.
Copy code
val bar = foo ?: return println("foo is null!")
val bar2 = foo2 ?: return someReturnUnitMethod()
d

diesieben07

06/19/2017, 11:14 PM
shiraji: You can use
run
for an ad-hoc block:
Copy code
val bar = foo ?: run {
    // do stuff here
    valueToUseForBar // last expression in a lambda is the value of that lambda 
}
s

shiraji

06/19/2017, 11:18 PM
Interesting. I haven’t come up with using
run
z

zeljko

06/20/2017, 6:48 AM
Also
?: {doStuff()}.invoke()
or
?: {doStuff()}()
👍 1
I also wandered why is
run
called
run
.