[fpc-devel] New feature discussion: for-in loop
Alexander Klenin
klenin at gmail.com
Wed Oct 21 15:48:46 CEST 2009
On Thu, Oct 22, 2009 at 00:26, Michael Van Canneyt
<michael at freepascal.org> wrote:
>> I beg to disagree.This is completely backwards -- like saying that "while" loop is an
>> ugly hack to save some typing of "goto" operators.
>> Iterators and foreach loops are very important tools of structured coding.
>> The fact that "for..in" may be expressed in terms of lower-level
>> primitives
>> does not decrease its importance, just like existence of "goto" does
>> not decrease the importance of "while".
>
> Well, I think you can't compare the two cases.
>
> I see little gain in changing
>
> while Something(f) do
> F.Somethingelse
>
> to
>
> For f in Something do
> F.SomethingElse
>
> The number of lines is equal, the amount of typed characters also is equal.
> It's not more readable, either, IMHO.
I understand what this thread is very long and hard to follow.
However, could you please at least read direct the answers to your mails:
On Tue, Oct 20, 2009 at 20:25, Alexander Klenin <klenin at gmail.com> wrote:
> 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.
There is another thing:
On Thu, Oct 22, 2009 at 00:26, Michael Van Canneyt
<michael at freepascal.org> wrote:
> When there is a big difference between the following goto and the while loop
> it is supposed to be equal to:
>
> :jumphere;
> If Something(F) then
> begin
> F.SomeThingElse;
> Goto jumphere;
> end;
>
> Here you need quite a few more lines and even an extra label definition, and
> I don't think anyone will dispute that the while loop is more readable...
The main advantage of "while" as compared to "if + goto" is not the code size
(there are times when using "goto" leads to shorter code),
but in the fact that it represents a more structured approach to coding,
reducing a probability of mistakes and easing code maintenance.
This is the main argument in favor of "for..in" as compared to "try +
while + manual iterator calling".
--
Alexander S. Klenin
More information about the fpc-devel
mailing list