[fpc-devel] for-in-index loop

Alexander Klenin klenin at gmail.com
Thu Jan 24 22:26:54 CET 2013


On Fri, Jan 25, 2013 at 7:56 AM, Florian Klämpfl <florian at freepascal.org> wrote:
>>> Just an example: what happens with i if I start to delete from s during
>>> the for loop?
>>
>> Exactly the same thing as in the current for-in loop: either a range check error
>> of nothing, depending on what you delete.
>
> How do you ensure this if I delete a char already processed? Will i be
> adjusted? Or will the loop continue to run with an invalid i?

Depends on whom you mean by "you". If you mean the programmer who uses
the feature,
then the answer is the usual one -- "by thinking". If you mean the
compiler, then the answer is also
the usual one -- "you can not". Consider these examples:

for i := 1 to Length(s) do begin
  Writeln(i, ' ', s[i]);
  DeleteSomePartOfS;
end;

i := 1;
for c in s do begin
  Writeln(i, ' ', c);
  DeleteSomePartOfS;
  i += 1;
end;

for c in s index i do begin
  Writeln(i, ' ', c);
  DeleteSomePartOfS;
end;

in all three cases, the effect will be more-or-less the same.
(Actually, my testing demonstrated that for-in loop does NOT cause an
exception here,
because s is copied by the loop. Paul, why did you code it that way?)

--
Alexander S. Klenin



More information about the fpc-devel mailing list