https://kotlinlang.org logo
i

ilya.gorbunov

10/31/2016, 3:22 PM
@menegatti in the following example
Copy code
x?.let { y ->
   doSomeNetworkCall(
       { 
           response -> x?.doSomething() // x could be null here, but y couldn't
       }
   )
}
The outermost lambda is inlined, because
let
is inline function, but the innermost is not, as
doSomeNetworkCall
is not inline (as I suppose). So the innermost lambda is represented as an anonymous class instance, which holds references captured in the closure. If
x
is a nullable property of some object, the closure captures instance of that object, so you could get it's
x
property value later, and the value of that property could become
null
at that point. And
y
is a local non-nullable val, and after it's captured in closure, you can still be sure that it remains not null in that closure.
👍 1