phldavies
08/10/2022, 9:19 AMx?.let { ... }
to if (x != null) { … }
in the coding conventions. Am I right in thinking the preference of ?.let
would be when the result is used as an expression, but if (!= null)
is better served for control-flow?Klitos Kyriacou
08/10/2022, 9:40 AM(very + complicated() * expression)?. let { ... }
instead of
val x = (very + complicated() * expression)
if (x != null) { ... }
Because unfortunately you can't do this (something that can be done e.g. in C++):
if ((val x = ...) != null) { doSomethingWith(x) }
Rick Clephas
08/10/2022, 12:23 PMI also have a general personal guidance of “prefer whichever one reads better”.That’s mostly what I do as well. You can actually do something like that with a `when`:
when (val x = a) {
null -> { println("x is null") }
else -> { doSomethingWith(x) }
}
Ubi
08/10/2022, 2:30 PMval
then it is does not matter but if x is a var
then use x?.let{}
.Alex Cruise
08/10/2022, 3:03 PM?.let
Jacob
08/11/2022, 2:38 AMx?.let{doSomethingWithoutX()}
then you'd be better off just doing an if