What's the idiomatic way to conditionally apply a ...
# getting-started
j
What's the idiomatic way to conditionally apply a
with
to a potentially null argument? I have code looking generally like
Copy code
with(myObject.statistics) {
   debug.addNumber(stat1)
   debug.addNumber(stat2)
}
but the
statistics
can be null if
myObject
isn't in the right state.
I can add a
if (this != null)
block inside the
with
but that feels clunky?
a
Why not swap it out for
run
?
Copy code
myObject.statistics?.run {
    debug.addNumber(stat1)
    debug.addNumber(stat2)
}
3
j
That works, thank you!