discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: PROPOSAL: Objective-C++


From: David Relson
Subject: Re: PROPOSAL: Objective-C++
Date: Mon, 19 Nov 2001 22:04:43 -0500

At 09:49 PM 11/19/01, Erik M. Buck wrote:
> I learned Objective-C after 3 years of (nearly) exclusive use of
> Smalltalk.  I was pleased to get the added speed of C's primitive types
> (int, float, etc) and loops, but really missed blocks.  A very simple use
> of a block is to apply a operation to all elements of a collection
(array),
> for example:
>
> - (void) arrayDo: (NSArray *) array
> {
>          [ array do: [ :item | item doSomething ]];
> }
>
> This example can currently be coded in Objective-C, but isn't nearly as
> elegant:
>
> - (void) arrayDo: (NSArray *) array
> {
>          int i;
>          for (i = 0; i < [ array length ]; i += 1 )
>          {
>                  NSObject *item = [ array objectAtIndex: i ];
>                  [ item doSomething ];
>          }
> }
>

I personally like

[array makeObjectPerformSelector:@selector(doSomething)];

Eric,

Yep. That's another way to skin the same cat. However, makeObjectPerformSelector: requires defining an additional method. Using blocks does it inline, which I consider a bit easier. Also, blocks can have two arguments. For example to sort an array one could use:

array = [ array orderedBy: [ :a :b | [ a lt: b ]]];





reply via email to

[Prev in Thread] Current Thread [Next in Thread]