From mboxrd@z Thu Jan 1 00:00:00 1970 MIME-version: 1.0 Content-transfer-encoding: 7BIT Content-type: text/plain Date: Wed, 2 Sep 2009 08:20:52 -0700 From: Roman V Shaposhnik In-reply-to: <09650C1A-A4C8-4030-81D6-9AC8913970A2@kix.in> To: Fans of the OS Plan 9 from Bell Labs <9fans@9fans.net> Message-id: <1251904852.16936.3250.camel@work.SFBay.Sun.COM> References: <09650C1A-A4C8-4030-81D6-9AC8913970A2@kix.in> Subject: Re: [9fans] "Blocks" in C Topicbox-Message-UUID: 5efdbe98-ead5-11e9-9d60-3106f5b1d025 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.