Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2011-03

Assignment to for..in loop variable

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Alexander Klenin

Currently, this code:

var
  i: Integer;
  a: array [1..10] of Integer;
...
  for i in a do i += 1;

Is compiled and silently does nothing.

Assignment to the for..in loop variable is currently not supported by Delphi either:
http://docwiki.embarcadero.com/RADStudio/en/Declarations_and_Statements#Iteration_Over_Containers_Using_For_statements

however, there are some good use cases for this:
http://stackoverflow.com/questions/2246087/delphi-variable-used-in-the-for-in-cannot-be-assigned

So, I have a bug report/feature request:

1) In the current implementation, FPC should at least warn on attempt to change for..in loop variable
(I assumed that since it compiles, it works, and it cost me much time to debug this)

2) Ideally, FPC for loop variable should have the same semantics as var parameter,
so changing it should change the underlying data.

Alexander S. Klenin

Reply by email

Jonas Maebe
to Alexander Klenin
Alexander Klenin wrote
1) In the current implementation, FPC should at least warn on attempt to change for..in loop variable (I assumed that since it compiles, it works, and it cost me much time to debug this)

It is forbidden when you use regular for-loops (except for in TP mode for compatibility). It's a bug that it is allowed for for-in loops, so please file a bug report.

Jonas

Reply by email