discuss-gnustep
[Top][All Lists]
Advanced

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

Re: Fast enumeration


From: Dr. H. Nikolaus Schaller
Subject: Re: Fast enumeration
Date: Mon, 7 Feb 2011 20:02:25 +0100

Am 07.02.2011 um 16:38 schrieb Jens Ayton:

> On Feb 7, 2011, at 16:19, Dr. H. Nikolaus Schaller wrote:
>> 
>> 
>> Just another idea I would like to propose for discussion
>> is to follow the "bracket-style" of NS_DURING NS_HANDLER & NS_END_HANDLER
>> macros (http://www.cocoadev.com/index.pl?ExceptionHandling)
>> 
>> I.e. something like:
>> 
>> #define FOREACH(TYPE, VAR, COLLECTION) { NSEnumerator *_e=[(COLLECTION) 
>> objectEnumerator]; TYPE VAR; while((VAR = [_e nextObject])) {
>> #define FOREACH_END } }
>> 
>> The benefit would be that it appears to be a little more safe against errors 
>> than
>> 
>> if(i == 0)
>>      foreach(...)
>>              if(j == 0)
>>                      ...
>> else
>>      ...
> 
> The macros I posted have the same rules WRT braces as a normal for loop. If 
> you're going to use FOREACH_END, you might as well use FOR_END and IF_END 
> macros, or - more practically - require every if body to use braces.

Ok, since you rely on C99 syntax.

I am not sure if this is acceptable to everybody and we should
not break things if we can find a more general (although more ugly)
solution.

You said: "It requires C99 mode, but hey, it's 2011."

Yes., that is right, but if the new macro is used in a
fundamental framework like Base I think we should be
very careful about this decision.

> 
> 
>> Finally, I think it should also be possible to rename the internal
>> variable _e by macro name generation
>> so that nested enumerations have private enumerator variables.
>> 
>> Something like ... NSEnumerator *_e##VAR;
> 
> This isn't necessary; nested enumerations each have their own scope.

but they share the same variable name ooForEachEnum for the loop enumerator.
This may lead to compiler warnings of shadowing local variables. Isn't it?

BR,
Nikolaus




reply via email to

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