https://kotlinlang.org logo
#getting-started
Title
# getting-started
t

thhh

04/10/2021, 3:11 PM
Help me understand what is meant by "Case 2 doesn’t work with mutating properties, instead in case 1 the variable 
it
 is always not null." The text in quotation was in response to: What is actually the difference and what is the preferred to use: Case 1:
variable?.let { //... }
Case 2:
if (variable != null) { //... }
e

ephemient

04/10/2021, 3:16 PM
if it's non-local or captured, another thread could be mutating the var
if it has custom getter or delegation then it could change at any time regardless
t

thhh

04/10/2021, 3:26 PM
Thank you! Is there a code snippet so that I could practically run and see Kotlin throwing NPE when another thread interferes or delegation as you mentioned.
n

nanodeath

04/10/2021, 3:32 PM
I mean, if
variable
is a field, you can imagine another thread calling
setVariable
e

ephemient

04/10/2021, 3:48 PM
easy to imagine
t

thhh

04/10/2021, 4:29 PM
Thank you so much for great examples! This makes things very clear!
2 Views