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

Sergei Gorelkin sergei_gorelkin at mail.ru
Wed Oct 21 18:06:49 CEST 2009


Alexander Klenin wrote:
> On Thu, Oct 22, 2009 at 02:07, Sergei Gorelkin <sergei_gorelkin at mail.ru> wrote:
>> As I tried to say earlier, having distinct StepNext() and Current()
>> functions is somewhat redundant, except the purpose of Delphi compatibility.
>> Rationale: the for..in loop manages the iterator object itself, and does not
>> allow user code inside the loop to access the iterator and call its
>>  methods. Therefore, it is possible to reduce the whole thing to:
>>
>> function Next(out value: SomeType): Boolean; iterator;
>>
>> thus eliminating the need for additional qualifiers after 'iterator'.
> 
> This is slightly better, but IMO does not worth additional incompatibility.
> Also, your proposal is far less extendable:
> 1) Delphi iterators have also magic 'Reset' function which is easy to
> express by attribute "iterator reset",
> but hard to express in your proposal

Reset is probably targeted for using iterators 'standalone'. I don't see 
how it fits into the for..in loop, which does not provide access to the 
iterator object.

> 2) See the end of the for-in wiki page for my proposal for optional
> "iterator index" extension,
> which is also non-trivial to do in your proposal.
> 
I doubt in usefulness of the iterator index. The iterators are, by 
definition, targeted for containters that are not indexable, otherwise 
just access container elements by index and be done with it.
Attempting to introduce the index, at best you get a simple counter, at 
worst there will be some number dependent on particular iteration 
method, so that a single element gets different index in different 
situations. Anyway, if the container is non-indexable, this index has no 
further use.


Regards,
Sergei



More information about the fpc-devel mailing list