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.

_______________________________________________
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply via email to