3 ms·
There are indeed some nuances you are missing as well as at least one misconception you are holding: > The idea that I can touch variables that have gone out o
by merlincorey 7y ago
There are indeed some nuances you are missing as well as at least one misconception you are holding:
> The idea that I can touch variables that have gone out of scope (and that have ostensibly been GC'd)
You can't touch variables that have been garbage collected, indeed.
A variable captured in a closure will have a reference to it and therefore not be garbage collected until the closure itself goes out of scope and is garbage collected.
Closures allow encoding some state into a function, which is especially useful for higher order functions that return specialized functions, for example. This is, for example, how decorators in Python work - a function is captured in a closure, which is then referenced and called by a new function wrapping the original function.