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

Michael Van Canneyt michael at freepascal.org
Wed Oct 21 16:15:08 CEST 2009



On Thu, 22 Oct 2009, Alexander Klenin wrote:

> 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.

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;

>
> 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".

If the try/finally is not needed (see above), then your argument is almost void, 
and IMHO not worth the effort.

Let's be serious: it should be clear that I am not a fan of iterators 
- just like I think generics is a waste of time - but it looks like they 
will be implemented, since there seems to be quite some momentum built up 
in favour of them. Just as it was for generics.

My only worry now is to make sure that if they are implemented, that we make 
the design as clean as possible: e.g. No hardcoded dependencies on class or
interface names.

Michael.



More information about the fpc-devel mailing list