[fpc-devel] fpc-devel Digest, Vol 178, Issue 38

Franz Müller office at focusdata.at
Wed Feb 20 19:31:54 CET 2019

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


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20190220/a041d3ce/attachment.html>

More information about the fpc-devel mailing list