......
But the fact of the matter is that for and its anomalous behaviour is defined in the language ** , cannot be easily removed, and results in behaviour that the compiler cannot easily warn (or error) as being unsafe.
......
i.e. despite its unfortunate behaviour it cannot be quietly dropped from the language implementation.
Sorry Mark, that does not make sense. The anomaly under discussion here is the value of a "for loop" control variable is some times undefined after loop termination ? Look at what existing code might do if the compiler is "fixed" so that the var contains last value in the loop -
- Most existing code does NOT use the value of the for loop control variable after the loop terminates, so, the change has no effect.
- Some (buggy ?) code may try to use the value after the loop. Wow, it will suddenly work as expected. It could not have behaved predictably before so cannot be seen as "depending" on the value being undefined. Unless, maybe, its a random Number Generator ?
There are no other possibilities !
Further, assuming FPC is not changed to retain the value, having the compiler warn of the danger should be little different to how the compiler already warns if an uninitialized variable is used.
Lets be honest, a compiler is just another software tool. The rule is, it should behave as the user expects. So, either retain the variable's value or issue a warning.
Additionally, allow an in-line var declaration (yes, as C does) in a "for loop". Sure St. Nick may turn over in his grave but there are heaps of other far more significant changes that have been made that seem to be regarded as a good thing.
ThaddyMy second example is what most people "expect", but is extremely sloppy C with total disregard for proper scoping rules. ( Hey, It is C..!)
Thaddy, how can you get stuck into a C programmer for not using a construct that is, apparently, permanently denied to us Pascal users ?
:-*
Thaddy, are you saying that this code will compile under Delphi 12.1 CE ?
{$APPTYPE CONSOLE}
{$IFDEF FPC}{$ERROR Won't compile in FreePascal}{$ENDIF}
begin
for var i:integer := 0 to 9 do
writeln(i);
....
If so, what is the argument all about ? We see endless changes made with no other criteria than "Delphi Compatibility".
Davo