10 ms·
Built-in support for closures in Golang is indeed a nice feature. In contrast, in C you would have to explicitly allocate/deallocate closures if they are passed
by Hirrolot 4y ago
Built-in support for closures in Golang is indeed a nice feature. In contrast, in C you would have to explicitly allocate/deallocate closures if they are passed upwards the stack [1], and pack all environmental variables in a single structure (used externally as a `void` pointer). This all tends to be quite clumsy in real-world code.
The Rust support for closures is more pleasant than that of C but the innate `Fn`/`FnMut`/`FnOnce` dichotomy makes things more complicated.
[1] https://en.wikipedia.org/wiki/Funarg_problem#Upwards_funarg_problem https://en.wikipedia.org/wiki/Funarg_problem#Upwards_funarg_...
- lloydatkinson 4y agoI can’t think of any modern languages without closures.
- speps 4y agoZig...
- lloydatkinson 4y agoOuch
- dist1ll 4y agoYou can emulate closures with anonymous structs. struct { fn foo() { /**/ } }.foo If you want closures that capture values, you just need to define the appropriate struct. I'm pretty sure with proper use of comptime reflection and custom allocators you can build pretty performant and ergonomic closures (albeit with a lot less memory and type safety of course).