Slackbot
02/23/2019, 5:27 AMDico
02/23/2019, 5:32 AMDico
02/23/2019, 5:32 AMDico
02/23/2019, 5:33 AMa ?.let { } works the samejw
02/23/2019, 5:34 AMDico
02/23/2019, 5:35 AMDico
02/23/2019, 5:37 AMjw
02/23/2019, 5:37 AMreturnjw
02/23/2019, 5:37 AMreturn? returnsNullable()
someSideEffect()
return? anotherNullableReturn()
otherSideEffect()
return whateverjw
02/23/2019, 5:38 AMreturnsNullable()?.let { return it }Dico
02/23/2019, 5:38 AMthrow?Dico
02/23/2019, 5:38 AM=? would be a bit betterDico
02/23/2019, 5:40 AM? works on non-nullable values. For example in a project where I make heavy use of inline classes because the code would otherwise run extremely slowly.Dico
02/23/2019, 5:40 AMDico
02/23/2019, 5:46 AMnull. In other words, there would be heavy restrictions.
As for your question, because it places ? in a more trailing position, and it's more consistent with return? that Jake proposed.
?= could be applied like:
myVar?=nullableValue
Which is confusing because it looks like ? is applied on the property more than with =? I would say.jw
02/23/2019, 5:46 AMmyVar?.foo()?Dico
02/23/2019, 5:48 AMmyVar?.foo() the subject of the question mark is myVar. Not the . following it.Dico
02/23/2019, 5:48 AMmyVarjw
02/23/2019, 5:48 AMDico
02/23/2019, 5:49 AMjw
02/23/2019, 5:56 AM?, right? also it's for the Result type and error propagation which is slightly differentDico
02/23/2019, 6:00 AMDico
02/23/2019, 6:01 AMoperator fun Result<*>.isSpecialNullValue(): BooleanDico
02/23/2019, 6:01 AMjw
02/23/2019, 6:04 AMreturn!Dico
02/23/2019, 6:04 AMresult??Dico
02/23/2019, 6:05 AMjw
02/23/2019, 6:05 AMDico
02/23/2019, 6:06 AMDico
02/23/2019, 6:07 AMjosephivie
02/23/2019, 6:17 AMreturn someValueThatCouldBeNull ?: skip and x = someValueThatCouldBeNull ?: skip, perhaps? Where skip is like break and continue and return in that it interrupts execution and skips to the end of the statement?jw
02/23/2019, 6:22 AMreturn keyword. It would make reading
return reallyReallyReallyReallyReallyReallyReallyReallyReallyLongLine() ?: skip
println("Hi!")
hard to understand when scanning verticallyjosephivie
02/23/2019, 6:24 AMjosephivie
02/23/2019, 6:25 AMJordan Stewart
02/23/2019, 7:35 AMjw
02/23/2019, 7:39 AMlittlelightcz
02/23/2019, 9:52 AMreturn sequence {
yield(returnsNullable())
someSideEffect()
yield(anotherNullableReturn())
otherSideEffect()
}.filterNotNull().firstOrNull() ?: whatever
This is a very raw sketch, so maybe it could be wrapped into something more simpler and meaningful 🙂littlelightcz
02/23/2019, 5:03 PMfun nullResult(action: () -> Any) = null.apply { action() }
fun returnsNullable() = null
fun anotherNullableReturn() = null
fun someSideEffect() = 1
fun otherSideEffect() = 1
fun main(): Any {
val whatever = 1
return returnsNullable() ?:
nullResult { someSideEffect() } ?:
anotherNullableReturn() ?:
nullResult { otherSideEffect() } ?:
whatever
}Dico
02/23/2019, 5:21 PMlittlelightcz
02/24/2019, 8:53 AMreturn returnsNullable() ?: run {
someSideEffect()
anotherNullableReturn()
} ?: run {
otherSideEffect()
whatever
}
But I cannot think of any better version at the moment 😁DALDEI
03/02/2019, 10:05 PM