Where there can be hundreds of lines before and after the use of the variable, making figuring out to which scope it belongs quite hard.
My point. And the problem isn't just finding out "which scope", but finding them at all.
Inline variables are most often not used for scoping, they are used for convinience, you need a new variable, you define it where you are right now.
And also my point. It only adds convenience if you write spaghetti code, routines with a great amount of code lines. Because in a small routine its no effort (even without IDE tools) to add them on top (write your 10 lines proc, at the end go up, with one glance see all your vars and add them into the var section / at the same time giving your code a final check, that all your vars a typo free).
"typo free"
So if I have the misspelled "valeu" var, and I later type in "value" and the compiler error takes me to the "value" that isn't declared yet, then - with inline - I would just add there and then "var value" and loose the already assigned value. Making my code buggy. If I had to go to the top, I would have a fair chance to see the "valeu", and realize the real issue.
And this is the main issue that defining the variables on top wants to avoid, there should be one place where all the variables are, so you can see them in one glance, and not that you just add variables wherever and figuring out which variables are used by the function becomes a game of where is waldo.
+1
This with construct makes clear that this is a construct for scoping, not for creating new variables. If you need a variable you would still create one normally.
Well first of all, Pascal doesn't allow user defined scopes at the moment (afaik). We know that from the "interface" difference. Delphi releases them asap, FPC only at the end of the function.
If we do have code that is scoping sensitive, then that should be a new construct. ("with" already has a different meaning).
But even if we used "with". Scoping and declaration are separate items.
I.e. then it should be really considered, that you still declare your variable on top, in the var block, maybe with a modifier
procedure a;
var foo: integer scoped some_label;
begin {} end;
Scope could then be indicated inside the code.
One idea would be to bind scope to a label, that label could be given after the "scoped" modifier in the declaration.
The scope would be the ONE next statement after the label. (which would usually be a begin...end / either on its on, or as part of a loop/condition/...).
Mind you, I don't really see the need. (Except for helping with the interface ref count hack).
But, if it is about scope, well that way it would be following the Pascal style of pre declaration.
And it would even help the few, who according to their claims constantly use a loop index var without initialising it. Declare it scoped, apply the label before the loop, and the var is scoped to that loop, and that loop only. So this would solve it, and no inline var needed.
Also note that it does not really add new functionality, With already creates new temporary variables, just without a name:
with FuncReturningRecord do
Temp vars are used in a lot of places. That is a compiler internal. Some "with" statements do create a temp var, others may not (afaik).
Also in many cases "results" (e.g. managed, or record) of function calls need a temp var, even without "with". (afaik again)
Off course, most of the above does not specify if "scope" means
- visibility only: you can use the identifier in the scoped code only , and outside the scope will get an error
- lifetime: any finalization (if there is) will be guaranteed at the end of the scope
Mind that there are cases where lifetime can be restricting optimization. I.e. forcing the compiler to add a finalize block, even though it could use a later already existing finalize.
But of course, for the interface ref count hack, lifetime would be wanted. (then again, that is a hack...)