I saw the following comment in a S.O. post, and I'm intrigued:
why don't you use if for null checks?
a?.let{} ?: run{}
is only appropriate in rare cases, otherwise it is not idiomatic – voddan May 15 '16 at 7:29 best way to null check in kotlin?
Why is that construct "only appropriate in rare cases"?
The lead engineer for Kotlin says,
run allows you to use multiple statements on the right side of an elvis operator https://stackoverflow.com/a/51241983/6656019
although I admit that's not actually endorsing it as idiomatic. Both of these posts seem to be from very well respected S.O. Kotlin contributors.
The post that inspired the original comment mentions that the let
part of the expression is important if a
is mutable. In that case, you'll need a?.let{} ?: run{}
instead of if{} else {}
.
I find I like the "let Elvis run" construct. Should I avoid it in most cases?
Thanks for any insight.