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

Marco van de Voort marcov at stack.nl
Wed Oct 21 16:19:35 CEST 2009


In our previous episode, Michael Van Canneyt said:
> >>  finally
> >>    it.Free;
> >>  end;
> >>
> >> Now, that is quite e few keystrokes to save, not to mention that
> >> if item value is used more than once in the loop, SomeClass.NextValue must be
> >> stored in a variable, further bloating code.
> 
> The above is only for classes. No-one said that the iterator must be a
> class (e.g. objects are stored on the stack), so it can be reduced to:
> 
> var
>    it: TSomethingIterator;
> 
>    it := SomeClass.Iterator;
>    while it.HaveValue do
>       it.NextValue.Something;

Lightcontainers does something like this, but uses function syntax based on
records. HaveValue and NextValue are inline.

One could see such constructs using D2006 methods-in-records syntax though,
and afaik I have even seen it in the Delphi fora.



More information about the fpc-devel mailing list