https://kotlinlang.org logo
Title
u

ursus

05/06/2018, 12:11 PM
yea i do get when you assign to a property, or when you need a expression, but other than that im not sold on it, "?.let { }" why not just if != null
a

Andreas Sinz

05/06/2018, 12:17 PM
it allows you to scope something to a specific code block
u

ursus

05/06/2018, 12:23 PM
well any { } is a block with its own local vars
oh its not in kotlin, i see
s

SiebelsTim

05/06/2018, 12:38 PM
When you have a variable that is shared across threads,
if (x != null) x.doSmth()
is not safe, as x could've changed to null in the meantime.
let
uses a temporary to guarantee that it is not affected by other threads.
u

ursus

05/06/2018, 12:44 PM
yes ive been just shown that, thank you, so its equivalent to val y = this.x if (y != null) y.doSmth() right?
a

Andreas Sinz

05/06/2018, 12:44 PM
yes
u

ursus

05/06/2018, 12:45 PM
I knew that, but didnt realize it, thanks a lot
a

Andreas Sinz

05/06/2018, 12:47 PM
let { }
doesn't replace
if
, just use either one of them when its feasible
u

ursus

05/06/2018, 12:48 PM
yea for vals properties its whatever, for vars its useful, thnx