foreach, an analogy

Bill Baxter dnewsgroup at billbaxter.com
Thu Jan 25 23:07:23 PST 2007


janderson wrote:
> Walter Bright wrote:
>> Bruno Medeiros wrote:
>>> As of DMD now, the only advantage in 'foreach_reverse' is ephemerous: 
>>> it allows efficient reverse iteration of arrays.
>>
>> That's the most important use case.
>>
>>> But couldn't the compiler easily detect this:
>>>   foreach(Foo f; &fooarray.opApplyReverse) { ...
>>> and and compile it as if it were a:
>>>   foreach_reverse(Foo f; fooarray) { ...
>>
>> Yes, it could. But it looks like a hack. A little syntactic sugar 
>> makes a big difference.
> 
> 
> What about:
> 
> foreach (Foo f; array.reverse) {}
> 
> The compiler would optimize.
> 
> -Joel

Blast from the past!

In D, array.reverse calls the method 'reverse'.

Anyway, elsewhere in that thread I think Walter basically said, yes the 
compiler could probably be made to optimize such a thing, but it would 
be difficult.  And he doesn't seem to see anything wrong with 
foreach_reverse, so I guess he's not inclined to try to make it work 
that way.

--bb



More information about the Digitalmars-d-announce mailing list