Blocks In Objective Cee

Besides the TaskMaster?/PortableObjectCompiler? approach to implementing Blocks in ObjectiveCee, there is the possibility of an interpreter for a subset of ObjectiveCee. JoeOsborn has implemented such an interpreter, which can be downloaded from http://homepage.mac.com/being/ as part of OSFoundation.

Even though ObjectiveCee has no language support for blocks, various parties have undertaken to add this missing functionality(along with HigherOrderMessaging) to the language, either purely as objects(a la OSFoundation, or from MartinHaeckers? work) or as an additional preprocessor(as in ObjCBrowser, a code editor for ObjectiveCee based on Smalltalk's Browser object).

Currently, the parsing performed by OSFoundation's OSBlockParser is using the OSDepthScanner class, which is implemented using Foundation's NSScanner... but this may be replaced by a flex/yacc-generated parser soon.

Notable features of OSBlock: CurryingSchonfinkelling is implemented, via the -assumeArgument:atIndex: method. OSBlocks are created from an NSString and (if necessary) an NSDictionary(for context).

Problems with OSBlock: No support for C control structures, and limited support for non-object datatypes.(the latter is fixable fairly easily) Impossible to call C functions except, possibly, by means of function pointers(but I don't even know immediately how to do that, either...).

OSBlock works acceptably for my purposes, even though the parsing can be slow. But blocks can be created in advance and then curried as necessary, so that problem fades. OSBlocks, due to their nature as purely objects, and without a syntactical addition, are inconvenient to create.

The preprocessor in ObjCBrowser will take blocks within @{...} and create an instance, in place, of another Block implementation that uses function pointers internally. For this endeavour, I'll have to look into work done by DavidStes? and BradCox...

If anyone wants to see parse trees and the like here... tough. The objects are ugly and I don't feel comfortable letting the world see what a slob I am. (: The sources are available with OSFoundation, but don't judge my general coding style based on what's in there - it was a month or two ago, I was younger, more naive. ^_^

As an aside, ObjCBrowser may eventually gain RefactoringBrowser features(but don't go putting it into the list of RefactoringBrowsers yet. ^_^) and will have a Smalltalk-style Workspace.

For more info on any of these projects, e-mail me at being@mac.com.

-- JoeOsborn

Well, the lack of decent Block-Support in ObjC has pestered me for quite some time now - so I have gone ahead and implemented them - finally. My Blocks do not use any parsing at all, instead, they use the GCC to do all the heavy lifting. :)

Oooh! Do tell how it works and what it looks like. Sample code would be good. Is there a web page for it somewhere? -- IanOsgood

You can find a preview of the implementation at http://dwt.de.vu/ and you use them like this:

 id aBlock = BLOCK(id, id each, return [each doSomethingThatReturnsAnID]);
 id result = [aBlock value:someObject];
Of course there are shortcuts like UnaryBlock?, BinaryBlock? or a TestBlock? - you can call arbitrary c-functions, use any control structures or just use it as a closure.

AFAIK this is closer to SmallTalk Blocks than anything I have found in any other implementation but I'm still not content, as I don't know how to make it work with arbitrary primitive data-types as arguments or return-type (I can only do id-arguments and id or BOOL return values right now). If you have any ideas on that - then those would be most welcome as is of course any other feedback at spamfaenger (at) gmx.de -- MartinHaecker?


Anyone reading this should look into "clang"'s support for blocks... (see CeeLanguageFamilyFrontEnd)

-- skb


YaKnow?, this is ObjectiveCee. It should really have an "@" sign somewhere. :)

   [aCollection forEach: @(id it) {
        print([it asString]);
    }];


See also BlocksInManyLanguages


CategoryCee


EditText of this page (last edited June 19, 2013) or FindPage with title or text search