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

Paul Ishenin ip at kmiac.ru
Tue Oct 20 10:51:20 CEST 2009


Michael Van Canneyt wrote:

>> And what is so special in the class type?
> 
> First:
> For a for loop, you are guaranteed that the loop logic behaves correctly.
> A loop is equivalent to the following:
> 
> I:=StartValue;
> E:=EndValue;
> While I<=EndValue do
>   begin
>   // loop stuff
>   Inc(I);
>   end;
> 
> And you cannot change I manually during the loop.
> 
> You don't know this with an iterator since you depend on the implementation
> of the iterator. The loop could loop forever then...

Yes. For-in loop is not a for-to loop. While and Repeat loops can also 
loop forever. You can't protect a developer if his hands grow from the 
wrong place.

> Secondly:
> You promote a certain class/interface to a language feature. The 
> compiler then depends on the presence of a certain class with some 
> 'known' methods in the RTL.

Yes, I see this very bad too. That's why this discussion is started. But 
what can be suggested instead?

Maybe new type?

TSomeIterator = iterator(<iterated type>)
...
end;

or we can use some basic class:

TIterator = class
public
   function MoveNext: Boolean;
   property Current: <what type to return?>
end;

I don't like non-constructive criticism. If you don't like something 
please be ready to suggest something instead. And in this case something 
what can be used to implement for-in loop for classes and implements 
this delphi compatible (so 'for S in MyStringList do' will work).

Best regards,
Paul Ishenin.




More information about the fpc-devel mailing list