Recent

Author Topic: Please make your language more freedom  (Read 37049 times)

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Please make your language more freedom
« Reply #15 on: June 21, 2018, 10:27:38 pm »
Thr inline var should not be there to be like C, but to improve readability and prevent bugs caused by uninitialized variable.

After a for loop the loop variable must not be used without being reinitialized. "for var" makes sure you do not accidentally use the variable. Using a loop variable after a loop must be a compile error. It makes no sense that fpc compiles code that accesses a variable it knows to be invalid

And other inplace "var .. := .."  make sure the variable is initialized at its declaration. No one  needs a variable that is  uninitialized.
To a compiler engineer that makes no sense: E.G. The for part makes the implied variable always initialized and not only through type inference ...By default....Silly you... Did you really understand this....?
Usually you know your theory a bit better.... <Sigh, everybody wants to be a programmer <grumpy, can't help it,  >:D >:D > >
 That's a sheer lack of understanding how compilers work (not just Pascal compilers, any compiler!)
If you want this kind of variables (just loop invariants) these CAN be determined by the compiler based on the language and still be LL(1).
If it is a good choice, I doubt it, because of such ill informed comments.. (and you are not a troll, I know, it is also YOUR job to educate some people on this forum that have DWIM opinions.... :D :D ;D ;D O:-)).
« Last Edit: June 21, 2018, 10:38:32 pm by Thaddy »
Specialize a type, not a var.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9793
  • Debugger - SynEdit - and more
    • wiki
Re: Please make your language more freedom
« Reply #16 on: June 21, 2018, 10:42:41 pm »
Thr inline var should not be there to be like C, but to improve readability and prevent bugs caused by uninitialized variable.

After a for loop the loop variable must not be used without being reinitialized.
Sorry but that is wrong: https://www.freepascal.org/docs-html/ref/refsu58.html
Quote
If the loop was terminated prematurely with an exception or a break statement, the loop variable retains the value it had when the loop was exited.

So there a cases where the loop variable can be accessed after the loop.

And anyway many variables are only used in a part of the function for which they are declared. An no one has yet asked for an "un-var" to end the existence of a variable.



Besides this, and while I am strictly against it, but for the case that such a limited scope variable should be desired:
1) if all loops where to be restricted, then the "var" keyword would not be needed
2) in any case, since this is pascal and therefore types are required, it would have to be: for i:integer = 0 to 5  // it could be byte, or anything.

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Please make your language more freedom
« Reply #17 on: June 21, 2018, 10:53:14 pm »
Besides this, and while I am strictly against it, but for the case that such a limited scope variable should be desired:
1) if all loops where to be restricted, then the "var" keyword would not be needed
2) in any case, since this is pascal and therefore types are required, it would have to be: for i:integer = 0 to 5  // it could be byte, or anything.
For all intend and purpose, I completely agree with you,
For the second case: if a compiler sees it is an ordinal based on an ordinal construct, in many cases even the size of the ordinal can be determined.
The current compiler does do just that in many cases.
Even if that can not be determined, the crude max-size of ordinal is known to the compiler.

But indeed, I would never want that, even if possible. Not in Pascal.
Specialize a type, not a var.

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Please make your language more freedom
« Reply #18 on: June 21, 2018, 11:04:17 pm »
So there a cases where the loop variable can be accessed after the loop.
That's only for morons. The state is undetermined. Although the frivolous rely on it this has never been the case.
NOT in Freepascal NOR in Delphi. The value immediately AFTER loop ends is undetermined, although re- initialization with another value is allowed.(like in Delphi, but that has "some" determined behavior...) and that is documented in the same link you provided: "The value of the loop variable is undefined after a loop has completed or if a loop is not executed at all. If the loop was terminated prematurely with an exception or a break statement, the loop variable retains the value it had when the loop was exited.
"....in the case of FreePascal.  Unless you mean that you can force or misuse an exception or a break? (Which I would frankly gladly do when I was in my 20's.. 8-) )
You should not re-use a loop invariant immediately AFTER the loop and rely on a supposed value. Which is - with hindsight - even a strong argument to allow the silly syntax I proposed....(without the silly var)  O:-) O:-) 8-) 8-) because in that case you can not re-use it by default....Because it is not declared... :D :D
(And use after free, technically.... >:( :-[ :-X :-X)
Code: Pascal  [Select][+][-]
  1. var i:integer;
  2. begin
  3.   for i := 0 to 9 do  writeln(i);
  4.   writeln(i);
  5.   for i := 0 to 9 do
  6.   begin
  7.     writeln(i);
  8.     if i = 9 then break;
  9.   end;
  10.   writeln(i);
  11. end.

Run the same in Delphi.... where 9 becomes 10....

« Last Edit: June 21, 2018, 11:55:30 pm by Thaddy »
Specialize a type, not a var.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Please make your language more freedom
« Reply #19 on: June 22, 2018, 12:39:13 am »
So there a cases where the loop variable can be accessed after the loop.
Indeed, since introduction of Break and Exit (and any other early exit mechanism, if any). The original design perhaps considers range boundary, i.e.: in Pascal it's possible to create a for loop that iterates from Low(SomeIntegerType) to High(SomeIntegerType), that doesn't have while loop counterpart because that High() expression results in maximum value of the type, therefore +1 will wrap it around (or range check error if activated), hence the idea of making it undefined after the loop instead to avoid people reusing the unexpected value.

Pasqualish

  • Jr. Member
  • **
  • Posts: 68
Re: Please make your language more freedom
« Reply #20 on: June 22, 2018, 07:29:22 am »
There's PascalABC.NET too. I liked it more from Object Free Pascal, not because it can use the full power of .NET but because I found their Pascal Dialect to be more modern and freedom than yours (just my own opinion).

(I still fail to see what "modern" has to do with imitating 1970s C syntax. The two years that Pascal is older than C can't matter that much?)

C clumped variable declarations at the top of blocks for the first 28 years of its existence. It wasn't until the C99 standard (1999 - 19 years ago) that it was decided to allow C variables to be declared anywhere in the code. So it would be emulating 1999 C syntax,  not 1970's.

Pasqualish

  • Jr. Member
  • **
  • Posts: 68
Re: Please make your language more freedom
« Reply #21 on: June 22, 2018, 07:31:28 am »
Thr inline var should not be there to be like C, but to improve readability and prevent bugs caused by uninitialized variable.

After a for loop the loop variable must not be used without being reinitialized. "for var" makes sure you do not accidentally use the variable. Using a loop variable after a loop must be a compile error. It makes no sense that fpc compiles code that accesses a variable it knows to be invalid

And other inplace "var .. := .."  make sure the variable is initialized at its declaration. No one  needs a variable that is  uninitialized.
To a compiler engineer that makes no sense: E.G. The for part makes the implied variable always initialized and not only through type inference ...By default....Silly you... Did you really understand this....?
Usually you know your theory a bit better.... <Sigh, everybody wants to be a programmer <grumpy, can't help it,  >:D >:D > >
 That's a sheer lack of understanding how compilers work (not just Pascal compilers, any compiler!)
If you want this kind of variables (just loop invariants) these CAN be determined by the compiler based on the language and still be LL(1).
If it is a good choice, I doubt it, because of such ill informed comments.. (and you are not a troll, I know, it is also YOUR job to educate some people on this forum that have DWIM opinions.... :D :D ;D ;D O:-)).

The poster was talking about using the for loop variable after the loop, not during the loop. Usage of the variable is limited to the scope of the loop if it is defined only for the loop.

Pasqualish

  • Jr. Member
  • **
  • Posts: 68
Re: Please make your language more freedom
« Reply #22 on: June 22, 2018, 07:32:57 am »
Clumping variable declarations together is an old school style - COBOL, Fortran, K&R C, Pascal, to name a few. C went away from it. Don't know about Fortran and COBOL. Would be interesting to know if they did.

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Please make your language more freedom
« Reply #23 on: June 22, 2018, 07:50:07 am »
indeed C99.
Btw how about such syntax instead of var, uses range:
Code: Pascal  [Select][+][-]
  1. // if it must be done you can avoid even the index variable completely like so
  2. for 0..9 do ..
  3.  
No chance the index variable can be re-used...
Specialize a type, not a var.

kupferstecher

  • Hero Member
  • *****
  • Posts: 583
Re: Please make your language more freedom
« Reply #24 on: June 22, 2018, 08:57:55 am »
Thaddy, as additional feature this would be ok.
If you don't have a variable, why bother with bounds?
"for 9 do.." or "for 9-4 do.."

But of course without iteration variable such a loop is useless in many cases. That by the way also limits the usecases of "for..in" loops imo drastically.

mercurhyo

  • Full Member
  • ***
  • Posts: 242
Re: Please make your language more freedom
« Reply #25 on: June 22, 2018, 09:10:05 am »
for var n=1 to xxx inside other languages should become
for n:integer=1 to xxx or
for n:word=... etc
for Obj:TSomeClass in TSomeContainer... do...
to follow pascal insight logic as typed consts declarations do, and to stay 'typed' language

yes, that could be cool, with for loops only syntax, because the ability of declaring variables anywhere would break 'structured' paradigm

we could call these constructs 'implicit iterators' something... being a PLUS to FPC

identifier 'n' should also be local to the loop block and not directly accessible in the func/proc scope

procedure myproc;
var lpresult: integer;
begin
 for n:integer=1 to 10 do begin
  if n>6 then begin
   lpresult:=n;
   break;
  end;
 end;
 n:= n+1; // compiler error unknown local var
lpresult:= lpresult +1; // correct!!! => 8
end;
« Last Edit: June 22, 2018, 09:49:41 am by mercurhyo »
DEO MERCHVRIO - Linux, Win10pro - Ryzen9XT 24threads + Geforce Rtx 3080SUPRIM
god of financial gain, commerce, eloquence (and thus poetry), messages, communication (including divination), travelers, boundaries, luck, trickery and thieves; he also serves as the guide of souls to the underworld

mercurhyo

  • Full Member
  • ***
  • Posts: 242
Re: Please make your language more freedom
« Reply #26 on: June 22, 2018, 09:46:00 am »
yes 'implicit iterators" (containers) could be pretty fun
« Last Edit: June 22, 2018, 02:10:59 pm by mercurhyo »
DEO MERCHVRIO - Linux, Win10pro - Ryzen9XT 24threads + Geforce Rtx 3080SUPRIM
god of financial gain, commerce, eloquence (and thus poetry), messages, communication (including divination), travelers, boundaries, luck, trickery and thieves; he also serves as the guide of souls to the underworld

mercurhyo

  • Full Member
  • ***
  • Posts: 242
Re: Please make your language more freedom
« Reply #27 on: June 22, 2018, 09:59:58 am »
oh .. let me think, it could also adapt to while blocks  :D

while (n:integer=0) < 1549 do begin
....
DEO MERCHVRIO - Linux, Win10pro - Ryzen9XT 24threads + Geforce Rtx 3080SUPRIM
god of financial gain, commerce, eloquence (and thus poetry), messages, communication (including divination), travelers, boundaries, luck, trickery and thieves; he also serves as the guide of souls to the underworld

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Please make your language more freedom
« Reply #28 on: June 22, 2018, 01:06:45 pm »
Thaddy, as additional feature this would be ok.
If you don't have a variable, why bother with bounds?
"for 9 do.." or "for 9-4 do.."

But of course without iteration variable such a loop is useless in many cases. That by the way also limits the usecases of "for..in" loops imo drastically.
Because range parsing is already there and my suggestion is therefor possibly easier to implement because of that, doesn't need/hardly needs a new syntactical element, just re-interpretation of two already present elements.
I would probably suggest to limit such a feature to a for loop only.
« Last Edit: June 22, 2018, 01:09:23 pm by Thaddy »
Specialize a type, not a var.

mercurhyo

  • Full Member
  • ***
  • Posts: 242
Re: Please make your language more freedom
« Reply #29 on: June 22, 2018, 01:35:21 pm »
last words... nesting

imagine.. vect1 and vect2 => managed arrays [x;y;z] like for example Tgamebig3dvectortables with an '*' overloaded operator

for x:integer= 0 to l1 do for y:integer=0 to l2 do for z: integer = 0 to l3 do
begin
 vect1[x,y,z]:=vect1[x,y,z]*vect2[x,y,z];
end;
writeln (x,y,z); //compiler error EBP released some stack  :P still unknown local vars

yep such structured and typed syntax would be definitely FUN and Useful
« Last Edit: June 22, 2018, 02:04:49 pm by mercurhyo »
DEO MERCHVRIO - Linux, Win10pro - Ryzen9XT 24threads + Geforce Rtx 3080SUPRIM
god of financial gain, commerce, eloquence (and thus poetry), messages, communication (including divination), travelers, boundaries, luck, trickery and thieves; he also serves as the guide of souls to the underworld

 

TinyPortal © 2005-2018