4 ms·
How do closures work in a language that has pass-by-value semantics? Does it copy all the closed-over data into the closure itself? Is the closure itself always
by jganetsk 17y ago
How do closures work in a language that has pass-by-value semantics? Does it copy all the closed-over data into the closure itself? Is the closure itself always heap-allocated and pass-by-reference, or is it pass-by-value?
- btilly 17y agoI'm not sure why you think there is a problem to be solved. When you execute some code you bind an environment to that code. (This binding step is necessary if you want recursion to work.) In the case of a closure you just leave the environment bound to the function that returns. It is irrelevant how data got into the environment - as long as it is there and remains bound to that scope, the closure will work. To be able to do this the environment needs to exist on the heap. However when you add the concurrency semantics that Go has, allocating things on the heap makes a ton of sense in general.