back

by uecker·1d ago·view on hn ↗
GCC's nested functions can also capture variables by reference. A closure combines the function with the environment which is essentially what my wide pointer is that contains the static chain that points to the environment. But for full support of first-class functions you would want return functions even below the level of where the captured variables live which is not possible with GCC's nested functions because they are on the stack and then go out of scope. So yes, this would require moving them to the heap and generally GC. Which is why the attempts to put C++'s "lambdas" into C are problematic, because naively copied lambda design will work even less well in C compared to C++ where you at least have smart pointers.

If you are interested, I explore the design space here. https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3654.pdf