you're out of luck then, for most programmers it d...
# announcements
c
you're out of luck then, for most programmers it does 🙂
r
can you than give some examples when continue the chain with null make sens?
k
Your toString example from earlier.
toString
is defined on
Any?
.
r
you can solve this with
a?.b.toString().length ?: null.toString()
k
There's nothing to solve, that's just how toString works.
You could define your own functions on nullable types as well.
r
So, no use cases, just mine invented 😞 This feature was not designed well
e
I think a better example will be
a?.b.c.d
a?.b
can be non-null, but how can you be sure
b.c
is also non-null? Think using a general case will make more sense.
c
besides
toString()
here's something from code I've seen in production (dumbed down for brevity)
Copy code
fun prepareNextActionCommand(input: ParamsObject): Command {
    fun null.toCommand() = DefaultActionCommand()
    return input.action.toCommand().apply { /* stuff */ }
}
r
@Czar you can solve this by adding
?: null.toCommand()
c
nope, I can't, because you're proposing to break chain on
null
, so if your version is accepted,
null.toCommand()
will not execute
toCommand()
and will instead return
null
r
?:
doesn't mean if left is null execute right?
e
Yes it does
r
then if left will be reduced to null the
null.toCommand()
will be executed, as needed
c
I don't see how
null.toString()
won't be executed and
null.toCommand()
will 😄
r
they both will be executed if they are at right at
?:
c
oh, so not only you're proposing hidden chain-break, you're also saying that this chain-break should not occur in expression if it is to the right of
?:
Too much non-obvious behaviour in my opinion.
r
it is not hidden, you know it will break at first null it always break at first null and reduce to null so
a?.b.toString()
will be reduced to
null
and we will have
null ?: null.toString()
everything works
c
?:
operator changing how method chaining works does not make sense to me personally