Blocks in Obj-C (was: Passing Obj-C method as callback to C function)
Blocks in Obj-C (was: Passing Obj-C method as callback to C function)
- Subject: Blocks in Obj-C (was: Passing Obj-C method as callback to C function)
- From: email@hidden
- Date: Wed, 29 Oct 2003 08:52:44 -0700
On Oct 29, 2003, at 2:02 AM, Christian Brunschen wrote:
The lack of a real need for Smalltalk-style Blocks, probably. After
all,
in Smalltalk, Blocks are needed for basic things like iteration,
conditionals, etc. Objective-C, however, uses C's constructs for those,
removing the need for Blocks.
Yes, but blocks are vastly superior for iteration. You don't have to
specify start and end conditions so you can't screw them up, plus there
are the query ops in Smalltalk collections like #select:, #collect:,
#detect: that have no analog presently. For instance:
result = [array select: @[:ea | ea intValue > 5]];
vs (off the top of my head - something like)
result = [NSMutableArray array];
NSEnumerator* i = [array objectEnumerator];
id item;
while(item = [i nextObject])
{
if([item intValue] > 5)
{
[result addObject: item];
}
}
Things like [array do:...] can actually be done differently, using
concepts like Higher-Order Messaging.
Yes, I'm familiar with Marcel's library. And its really brilliant
considering the constraints he was working under. But in the above
example, it wouldn't help because HOM can only arrange to send a
message to each object and doesn't help if there's no method that does
what you want. You could stick one on using a category but now you've
written much more code than just doing it with C loops.
And indeed anything you might want to use a Block for can actually be
done
using a plain C function. So we already _have_ a construct, and don't
really need to introduce another one.
Yes, and you can fake a block in Java with an anonymous class - but it
takes too long to type all the extra crap, like making up a name,
naming the return type, etc...
Many uses of Blocks also lead to a code clutter: code that serves one
purpose being written within a method that actually does something
else.
In such cases I find it better to separate the code that would have
beel
placed in a Block, in a method or function of its own; this can then
also
be debugged and reused much more easily.
To each his own. I disagree. I think it would tend to keep the
namespace cleaner and makes the locality of the operation cleaner.
Thanks for your opinion though.
-Todd Blanchard
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.