Hi guys, I'm wondering if the `?.` operator thread...
# announcements
p
Hi guys, I'm wondering if the
?.
operator thread safe? for example I have a
var str: String? = null
, is it safe to call
str?.length
? could NPE happen?
j
It's equivalent to
Copy code
val localStr = str
if (localStr != null) {
  localStr.length
}
s
The ‘call’ to
?.
itself is ‘thread-safe’/‘atomic’. But afer that call, the global/instance variable you called it on, can be null again.
p
crystal clear, thanks!
a
a
let
on that would resolve the thread safety issue, right? My understanding is that
Copy code
str?.let {
it.length
}
would be safe since the
it
is a copy
j
there is no issue. that code is equivalent to
str?.length
👍 1
a
Huh, I honestly thought
let
made a local copy. Thanks!
Right, I see now
j
It does. So does
str?.length
. That was my point.
👍 2