sometimes you just want to use the result of the l...
# random
k
sometimes you just want to use the result of the left-hand-side inside of a lambda whether or not it's null
o
Not sure I got that, can you give an example?
Do you mean to use let for non nullable types?
v
veryLongExpression.let {println(it)}
This is the intended use of
let
?.let
was advertised as a get-away drug for Swift people who are used to
if let = .. {}
k
Right,
let
is used to capture the value of the left-hand-side into a param of a lambda. It's meant to be a replacement for constructs you see everywhere like:
Copy code
val storedValue = longComputation()
/* use storedValue */
that way you have scoping for that val and you don't end up with a bunch of local vals polluting your namespace
it's also an added bonus that because of
?.
you can use it for null-checking