According to Maurice Lombardi:
A side remark: this is a very stupid BP extension, and it is a pity that gpc allows it (except of course in compatibility mode --borland-pascal). What are the advantages of this "extension":
- saving a word of memory, but who cares nowadays of saving a single
word of memory (I don't speak of saving storage for large arrays)
In fact this is not the case since local variables are only temporary but global ones require static memory.
- saving some keystrokes. Many beginners would think so ...
While I agree that this is a clear disadvantage I see other applications for global variables used in `for' loops: Think of a global state of the program (stored in a global variable) that goes through some "phases" during the loop. (In other words: I claim that there are some situations where the side effect is intended.)
program essai;
var i:integer;
procedure test; begin for i:=1 to 2 do writeln(i) end;
begin for i:=1 to 4 do test; end.
In real life programming finding such a bug in the middle of a few thousand lines program is usually a headache, especially because the fact that something goes wrong is not so obvious. [...]
In real life I would never call a global variable `i' or place a loop of this type in the main program, but I see your point: The compiler should help to detect the forgotten local declaration.
How to proceed?
* In --borland-pascal mode, GPC must emulate BP's behaviour (which is the case).
* In --extended-pascal mode, GPC must forbid expressions and non-local variables used as `for' control variables (which is the case).
* In default mode ... what to do?
In fact I don't care too much about `for' because I usually use `while' instead (because I need additional conditions to exit the loop), but I don't think that GPC should be really strict. Beginners must be *tought* that saving keystrokes is not a goal in programming; it is not desirable to disable valuable features of the compiler in order to avoid some minor traps.
What we must decide for each extension is whether it is a valuable feature or a major trap.
Peter