[fpc-devel] New feature discussion: for-in loop

Alexander Klenin klenin at gmail.com
Tue Oct 20 10:54:25 CEST 2009


On Tue, Oct 20, 2009 at 19:40, Graeme Geldenhuys
<graemeg.lists at gmail.com> wrote:
> // for basic type we will call only the apropriate function
> procedure TraverseString(S: String);
> var
>  C: Char;
> begin
>  for C in S using GetReverseStringEnumerator(S) do
>    DoSomething(C);
> end;
> Now your for-in syntax changes because you want to use different
> enumerators. Why? It's not very elegant having to change your code,
> based on what you are iterating. That tells me it's not flexible
> enough. With the Iterators I wrote, you only have ONE interface you
> have to work with, no matter what type of list you are iterating or if
> the returned results are filtered or not.

This is just one of the proposed syntax variants (and not the one I
personally favor).
Current Delphi iterators do it with the same syntax:

for C in ReverseEnumerator(S) do
  DoSomething(C);


>>> and can't peak
>> That is true, but this is required in maybe 1% of the cases, or even less.
>> For these cases, certainly slightly longer while loop is justified.
> Lets say you want to parse a UTF-8 string. Or you want to parse text
> for a documentation generator, or you want to strip text from an HTML
> file or Wiki page. All uses of where Peak is very often used. And
> that's just a short list of the top of my head.

Hm. I can not see where lookahead aka peeking
is required in using any of the enumerators you mention.
UTF8String enumerator is even among the samples on the wiki page.

>> Please read again with more attention ;-)
> BTW: The wiki pages does not contain the word "reverseEnumerators" as
> you suggested earlier.

My bad. I skipped space between words.

-- 
Alexander S. Klenin



More information about the fpc-devel mailing list