[fpc-pascal] Syntax changes suggestions
Martin
fpc at mfriebe.de
Tue Jul 17 12:04:19 CEST 2018
On 17/07/2018 11:05, Henry Vermaak wrote:
> On Mon, Jul 16, 2018 at 03:02:42PM +0200, Sven Barth via fpc-pascal wrote:
>> Santiago A. <svaa at ciberpiula.net> schrieb am Mo., 16. Juli 2018, 13:41:
>>
>>> I have some suggestions of change to freepascal syntax, just to debate
>>>
>>> (All are backward compatible)
>>>
>>> - Declaring variables inside blocks, and loop variables
>>>
>> -> reduces readability -> no interest
> How can it reduce readability? You move variables closer to where they
> are used, therefore reducing clutter in the main "var" section.
> Limiting variable scope is definitely a good thing, I sure hope you're
> not arguing against that. That's why almost all languages encourage it.
>
On the contra side:
You could then have 2 or more differnt "i" variables in one procedure.
That is hell to read.
Of course you could argue that you can only have a "nested" "i", if
there is no local var "i" (current style local). And no other nesting.
Still 2 issues:
You could have 2 consecutive loops, with different typed "i". That can
be confusing. (it may not be to you, but is still can be). Now you would
need 2 different named loop counters (which to me is good).
But more troubling:
You do no longer have a complete list of all local vars. That means, if
I want to add a (current style) local var "i", i may have to scan the
entire procedure (maybe hundreds of lines) to find if there already is a
nested "i" (or start the compiler to see if there is an error).
That would be a real burden added to pascal (and since I may have to
work on other peoples code, it is no good to say that I could simply not
use it....)
----------------------
Also lets make a distinction here:
What are we looking for:
A) Saving the (so called) work, of declaring the var on top? (Note: that
the work is pressing ctrl-shift-c)?
B) Introducing vars with more fine grained scoping?
As for (A) I already expressed that I am against it. (there also is a
recent thread on the forum where this was discussed, so look it up and
read the arguments)
As for (B):
I don't know of any good proposal, but if there was it would have to be
something like this: (I still dont like it, but if there was a good idea
to bring it in shape...)
procedure Foo;
var
a: Integer; // normal vor
scoped var
b: integer;
begin
//b can not be used here / it will be as if it does not exist
a := bar();
if a > 1 then begin
scope b; // must be first after begin
b := 1;
end;
// b out of scope
// scope for one statement (the entire for, including the begin end
which as a compound statement is part of the for.
with scope b do for b := 1 to 10 do ... ;
// alternatively
using scope b do for b := 1 to 10 do ... ;
scope b: for b := 1 to 10 do ... ;
So you still declare the var on top.
Again, this is to entertain an idea, which I personally do not like....
If you have a scoping problem in pascal, cut your code into smaller
procedures.
More information about the fpc-pascal
mailing list