Ruckus
02/23/2019, 5:27 AM?
on more and more operators
If a property is not null, it would be nice to have a conditional assignment operator that would assign a nullable value only if it is not null. For example:
map[KEY]?.let { value = it }
would become
value ?= map[KEY]
Edit
As @Dico pointed out, value ?= map[KEY]
can be a bit confusing, as it seems to imply value
is the nullable item instead of =
. It may make more sense to use value =? map[KEY]
, but I'm on the fence.Dico
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 AMreturn
jw
02/23/2019, 5:37 AMreturn? returnsNullable()
someSideEffect()
return? anotherNullableReturn()
otherSideEffect()
return whatever
jw
02/23/2019, 5:38 AMreturnsNullable()?.let { return it }
Dico
02/23/2019, 5:38 AMthrow
?Ruckus
02/23/2019, 5:38 AMget
and invoke
operators, e.g.
nullableMap?[KEY]
nullableLambda?()
Dico
02/23/2019, 5:38 AM=?
would be a bit betterRuckus
02/23/2019, 5:39 AM.
in ?.
and :
in ?:
.Dico
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 AMRuckus
02/23/2019, 5:41 AM?
in Kotlin, it's for dealing with nullable values.Dico
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 AMmyVar
jw
02/23/2019, 5:48 AMDico
02/23/2019, 5:49 AMRuckus
02/23/2019, 5:50 AM?=
as I'm used to seeing ?
immediately before an operator.Ruckus
02/23/2019, 5:53 AMreturn?
is interesting. I like it more than rust (where ?
creates an implicit return anywhere it pops up; I'm not a fan of implicit returns)jw
02/23/2019, 5:56 AM?
, right? also it's for the Result type and error propagation which is slightly differentRuckus
02/23/2019, 5:59 AMDico
02/23/2019, 6:00 AMDico
02/23/2019, 6:01 AMoperator fun Result<*>.isSpecialNullValue(): Boolean
Dico
02/23/2019, 6:01 AMRuckus
02/23/2019, 6:03 AM?
operator onto results seems useless to me without some equivalent of Rusts Into
trait, and Kotlin is (usually) designed against implicit conversion.jw
02/23/2019, 6:04 AMreturn!
Dico
02/23/2019, 6:04 AMresult??
Dico
02/23/2019, 6:05 AMRuckus
02/23/2019, 6:05 AMreturn?
makes sense. I read that as "return if not null, else continue" as opposed to return!
which I read as "return if not not null, else throw".jw
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