On Wed, 2009-09-02 at 10:04 +0200, Anant Narayanan wrote: > Mac OS 10.6 introduced a new C compiler frontend (clang), which added > support for "blocks" in C [1]. Blocks basically add closures and > anonymous functions to C (and it's derivatives).
They are NOT closures in my book. They lack lexical scoping. A true closure makes the following possible (using JavaScript to stay closer to C syntax): function outer() { var outer_var = 1; return function () { outer_var = { simply: "a different object" } } } Notice how I can modify outer_var from within the closure. With blocks all you get is an "upvaluer" (IOW, outer_var becomes constant). This is still useful for things like map, etc. But I hate when people call anonymous classes in Java "closures" (although depending on a textbook it is possible to stretch the term). Finally, if you want to know how to get from GCC blocks to the real deal read up 6.6 of this excellent paper: http://www.lua.org/doc/hopl.pdf > Full details with examples are in the linked article. The article seems to miss a couple of important points. And the better alternative for those who want to know what's going on is Apple's developer website: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/Blocks/Articles/00_Introduction.html#//apple_ref/doc/uid/TP40007502-CH1-SW1 > I think the feature is quite > elegant and might be useful in cases where you want map/reduce like > functionality in C. It is a fun feature, no doubt. It is also pretty easy to implement. > How much effort would it be to support a feature similar to blocks in > 8c (and family)? What are your thoughts on the idea in general? Personally I think you'd be better off exploring a connection that a language called Lua has to C. In the immortal words of Casablanca it just could be "the begging of a beautiful friendship". Thanks, Roman.