How is the situation with closures?

Hello everyone!
Today I found out that there is no closure/arrow function in Zig. I found some proposals, all closed, but It’s very difficult for me from an external point-of-view to understand if there is or not the intention to include closures in Zig.

I read https://github.com/ziglang/zig/issues/1717#issuecomment-1627790251 where Andrew rejected the proposal previously accepted, what is the situation right now with the feature? Officially rejected forever?

I’d recommend starting with this thread if you haven’t seen it already: Anonymous functions/lambdas

You’ve probably gathered that it’s a contentious issue because of Zig’s explicit allocation model. Anonymous functions in other languages can be stateful, so we can talk about the “capture” of an anonymous function. Stateless anonymous functions are fairly easy to solve but are verbose (there’s examples in that link I sent you).

One last point about the language here - closures are used quite a lot in Zig (specifically if you are talking about type-erased callbacks). From your original post, it looks like what you’re referring to is a terse-syntax for defining function objects.

4 Likes

Yes, I was thinking about a more compact syntax, but the link you gave me sheds much light on why it is like it is right now and how to do classical closures in Zig.
After reading it, it makes sense after all!

Thank you very much!

2 Likes