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

Alexander Klenin klenin at gmail.com
Tue Oct 20 11:25:04 CEST 2009


On Tue, Oct 20, 2009 at 20:09, Michael Van Canneyt
<michael at freepascal.org> wrote:
> But I really don't see the advantage of being able to type
>
>  For F in Someclass.Iterator do
>    F.Something
>
> over
>
>  While SomeClass.HaveValue do
>    SomeClasss.NextValue.Something;
>
> It's not clearer, at most it saves you a couple of keystrokes.

This is because the while you provided is not equivalent to the for loop above.
The correct translation would be:
var
  it: TSomethingIterator;
...
  it := SomeClass.Iterator;
  try
    while it.HaveValue do
      it.NextValue.Something;
  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.

> Borland - in its desire to make money - implemented every idiots' request,
> that we should do the same.
Well, I have the same attitude regarding sealed classes ;-)

However, for..in is not like those.
Let me provide a few over-generalized statements in support of for..in feature.

1) Convenient syntax matters -- otherwise we would be programming in C
2) Language level matters -- otherwise we would be programming in
assembler language
3) for..in is both a convenient syntax and a very important high-level feature.
It is actually more fundamental and important than for..to loop, and it is only
a matter of weak compiler implementations that for..to loop was
introduced first.

-- 
Alexander S. Klenin



More information about the fpc-devel mailing list