3 ms·
> Is it literally just the language invisibly adding the parent-scope variables and their values to the top of my function? Pretty much. When implementing clos
by ufo 7y ago
> Is it literally just the language invisibly adding the parent-scope variables and their values to the top of my function?
Pretty much. When implementing closures, the variables that are going to be used by the nested functions are allocated on the heap instead of on the stack. The nested function is then set up to receive a pointer to the outer variables as an additional parameter.
If you want to read more about it, the word to search for would be "closure conversion"
http://matt.might.net/articles/closure-conversion/ http://matt.might.net/articles/closure-conversion/
It's unfortunate the the Wikipedia article for closure conversion now redirects to the article about Lambda Lifting. They are related but aren't quite the same thing.