Well one of the answer in the Cantu blog has this ( which I changed to lets say a "real world" relative big function ) :

var arr: array... function Fuckup: boolean; var I: Integer; begin  <....> 20 lines of code  for var I := Low(arr) to High(arr) do if arr[I]..... then break; <....> 20 lines of code  Result := (I <= High(arr)); end; regards, -- Dimitrios Chr. Ioannidis

It's a nice example why scoped variables would be in fact better, because the 
value of i is undefined if the for loop is exited normally. So even the original 
code wouln't work as expected, you cannot rely on i>High(arr) in that case, it 
depends on the way the for loop is implemented.
Using only the variable local to the loop would cause a compiler error, while 
using only the variable defined in the function body would not even cause a 
warning but leed to unwanted results.

On the other side, using a scoped variable while knowing that you will need the 
value of the value of the variable later is a kind of stupidity which will make 
this one a minor probem, compared to thers that such a programmer woud 
introduce into the code...

I don't know how inline variables are defined in Delphi, but forbidding to use 
names for inline variables which are already known at that place of the code, 
no matter where they have been defined, would eliminate this potential problem 
completely.
(And if such a restriction does not exist in Delphi, it could be removed just 
for $mode delphi to keep compatibility, while the other compiler modes would 
ensure code safety in that respect.)





_______________________________________________
fpc-devel maillist  -  [email protected]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply via email to