On 16/07/2018 16:46, Santiago A. wrote:
El 16/07/2018 a las 13:59, Michael Van Canneyt escribió:

But declaring variables inside code blocks makes for really bad readability and - worse - possibly error prone code.

What to do with scope rules ?

Var
  C : integer;

begin
  C:=1; // C is integer
  // New block, hence new scope
  for var c:string in List do begin
    ... // C is string
  end;

It is a local variable to the block, the scope is the from the declaration to the end of the block.
What is a block?
a) Each individual statement
b) a compound statement

** if a, then your examble
   begin
      var a: integer;  // statement ends at ";" the scope ends too
      foobar;

** if b:
Var
  C : integer;

begin // <<<<<<<<<< block "1" begins here
  C:=1; // C is integer
  // New block, hence new scope // <<<<<<<<<<<< see question above, where exactly is the new block?   for var c:string in List do begin // <<<<<<<<<< block "2" begins after/with the "begin" statement
    ... // C is string
  end;

var c:string is therefore part of block 1. And valid to the end of block 1, which includes the code after the loop.



Nevertheless, I think that declarations should be at the beginning of the block, before any executable statement, so the scope is the block.

well in your "for" loop example, that is not the case. the "var" is not at the start of the block. (whatever the block is).

So you already contradict yourself.

Besides that, the discussion has been had plenty of times already, most recently on the forum. So the question is not "why this feature", but "what makes this instance of the discussion different"?

And please do not say "if it is requested so many times", because if this was to be an argument, then I could request a million times to replace "begin" with "{", but at the same time keep "end" as keyword. If I request it often enough, it would have to be good, or would it not?



_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply via email to