4 ms·
You can declare an anonymous struct that has a function and reference that function inline (if you want). There's a little more syntax than a dedicated languag
by jmull 1y ago
You can declare an anonymous struct that has a function and reference that function inline (if you want).
There's a little more syntax than a dedicated language feature, but not a lot more.
What's "missing" in zig that lambda implementations normally have is capturing. In zig that's typically accomplished with a context parameter, again typically a struct.
- veber-alex 1y agoSo basically, Zig doesn't have lambdas, but because you still need lambdas, you need to reinvent the wheel each time you need it? Why don't they just add lambdas?
- flipgimble 1y agoBecause to use lambdas you're asking the language to make implicit heap allocations for captured variables. Zig has a policy that all allocation and control flow are explicit and visible in the code, which you call re-inventing the wheel. Lambdas are great for convenience and productivity. Eventually they can lead to memory cycles and leaks. The side-effect is that software starts to consume gigabytes of memory and many seconds for a task that should take a tiny fraction of that. Then developers either call someone who understands memory management and profiling, or their competition writes a better version of that software that is unimaginably faster. ex. https://filepilot.tech/ https://filepilot.tech/
- do_not_redeem 1y agoNothing about lambdas requires heap allocation. See also: C++, Rust
- porridgeraisin 1y agoIf the lambda captures some value, and also outlives the current scope, then that captured value has to necessarily be heap allocated.
- do_not_redeem 1y agoThat would be a bug, so just... don't do that? If you return a pointer to a local variable that outlives the scope, the pointer would be dangling. Does that mean we should ban pointers? If you close over a pointer to a local variable that outlives the scope, the closure would be dangling. Does that mean we should ban closures?
- noselasd 1y agoNo, (in C++) the lambda can capture the the variable by value, and the lambda itself can be passed around by value. If you capture a variable by reference or pointer that your lambda outlives, your code got a serious bug.
- gf000 1y agoAnd in Rust, it will enforce correct usage via the borrow checker - the outlive case simply will not compile. If you do want it, you have the option to, say, heap allocate.
- jmull 1y ago> So basically... Well, not really. Consider lambdas in C++ (that was the perspective of the post I replied to). Before lambdas, you used functors to do the same thing. However, the syntax was slightly cumbersome and C++ has the design philosophy to add specialized features to optimize specialized cases, so they added lambdas, essentially as syntactic sugar over functors. In zig the syntax to use an anonymous struct like a functor and/or lambda is pretty simple and the language has the philosophy to keep the language small. Thus, no need for lambdas. There's no re-inventing anything, just using the language as it designed to be used.