I had a thought yesterday, what if there was somet...
# language-proposals
s
I had a thought yesterday, what if there was something like a super elvis operator, for cases where there are many safe calls, but this feature is probably too overkill. 😛 For example, rather than this:
bob?.department?.head?.name ?: "meh"
There is something like this:
bob.department.head.name ??: "meh"
2
👎 8
o
May be
bob?.let { it.department.head.name } ?: "no bob"
☝🏼 1
s
Would that cover any null reference exceptions inside the .let scope?
o
Of course not, it will not compile if
department
or other properties are nullable. It might be suitable if
bob
is nullable, but other properties are not, and all these safe calls are because of bob
s
Ah yes, that would be cleaner for when it only propagates the null from bob. And I see the let is suggested in the docs https://kotlinlang.org/docs/reference/null-safety.html#safe-calls But I'm more pointing towards any of those variables are nullable in the chain
The hypothetical super elvis operator would be syntactic sugar for multiple safe call chains where any property is nullable.
d
I don’t like the idea of the behavior of the
.
operator changing based on some scoping mechanism.
3