3 ms·
Closures. This is the only thing that I miss in C. I always want to declare local functions, and return pointers to them.
by enriquto 11y ago
Closures.
This is the only thing that I miss in C. I always want to declare local functions, and return pointers to them.
- Gibbon1 11y agoYou are not alone in that. A lot of the annoying stuff in C could be fixed going down that path.
- eatonphil 11y agoGNU C supports nested functions. Does that solve it (if you accept GCC lock-in)? https://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html https://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html
- enriquto 11y agoFrom GCC documentation: "If you try to call the nested function through its address after the containing function exits, all hell breaks loose." I would definitely accept GCC lock-in, but so far GCC does not have this feature. Clang does, hoever, via _Blocks, but the syntax is not very C-like.
- gdwatson 11y agoThat would require implicit heap allocation, which is a lot to ask for in a language where malloc is a library function.
- jjnoakes 11y agoNot all closures require heap allocation.
- gdwatson 11y agoEnriquto asked for closures which remain valid after the enclosing function has returned — upward funargs. If the closed-over variables need to survive the parent’s stack frame, where else would you put them?
- jjnoakes 11y agoThe variables could be static, or optimized away, or... I'm not saying every closure can be done without allocation. I'm saying many useful ones can be though.