[fpc-devel] for-in-index loop

Florian Klämpfl florian at freepascal.org
Thu Jan 24 22:44:41 CET 2013


Am 24.01.2013 22:26, schrieb Alexander Klenin:
> 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".

"You": the designers of this extension.

> If you mean the programmer who uses
> the feature,
> then the answer is the usual one -- "by thinking".

Or a good possibilty to create hard to find bugs.

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

In the first two cases the programmer knows that he does something
strange, actually he can even adjust i if needed. In the last he might
expect that i is always correct because it is a language construct and
he has no change to fix i.



More information about the fpc-devel mailing list