Recent

Author Topic: Redeclared constant as variable  (Read 2204 times)

Peter H

  • Jr. Member
  • **
  • Posts: 57
Re: Redeclared constant as variable
« Reply #15 on: July 24, 2019, 08:37:12 pm »
Writable typed constants are much older than Object Pascal.

I know this. It comes from early Turbo Pascal.
But the attribute should be named "static" and not "const".
Because the variable is in a static initialized DATA segment. ;D
True Consts are ROMable and can reside in CODE or write protected CONST Segment, Pascal "const" not.


« Last Edit: July 24, 2019, 08:39:48 pm by Peter H »

Thaddy

  • Hero Member
  • *****
  • Posts: 9169
Re: Redeclared constant as variable
« Reply #16 on: July 24, 2019, 09:26:54 pm »
[I know this. It comes from early Turbo Pascal.
Wrong. It was even earlier. It is part of Niklaus Wirth's
Quote
But the attribute should be named "static" and not "const".
Although the concept of static existed at the time, it was not main stream. Neither Pascal, nor K&R  knew about it. As did many other languages except a similar syntax in COBOL.
Since there was no "static" it should stay const. Even ISO Pascal pre-dates the introduction of static in the curly brackets.

And, btw static and typed const are rather similar:one is C syntax for a Pascal typed const, do not confuse with const : in normal language they mean the same or very close to the same. Non-argument. Know your history.  Also do not confuse with static procedures and functions. That's again something completely different. You are confusing semantics.

To demonstrate a piece of curly brackets:
Code: C  [Select]
  1. #include <stdio.h>
  2.  
  3. void foo()
  4. {
  5.     int a = 10;
  6.     static int sa = 10;
  7.  
  8.     a += 5;
  9.     sa += 5;
  10.  
  11.     printf("a = %d, sa = %d\n", a, sa);
  12. }
  13.  
  14.  
  15. int main()
  16. {
  17.     int i;
  18.  
  19.     for (i = 0; i < 10; ++i)
  20.         foo();
  21. }
« Last Edit: July 24, 2019, 09:58:46 pm by Thaddy »
also related to equus asinus.

Peter H

  • Jr. Member
  • **
  • Posts: 57
Re: Redeclared constant as variable
« Reply #17 on: July 24, 2019, 09:54:20 pm »
So far I remember, Wirth's Pascal had no typed initialized constants, and if it had then these where not writable.

I remember early discussions in computer magazines, where this writable "feature" was considered a bug, and it should not be used, because Borland might remove it in future.

Anyway, any computing language has its historically grown oddities.
My first language was not Pascal. I learned it on Job after I learned as a hobby C and Modula II on my Amiga ;-)
So let it be.

Thaddy

  • Hero Member
  • *****
  • Posts: 9169
Re: Redeclared constant as variable
« Reply #18 on: July 24, 2019, 10:02:57 pm »
So far I remember, Wirth's Pascal had no typed initialized constants, and if it had then these where not writable.
It made it int the ISO standard. You may have missed my edit where I added an Ansi  C example (Ansi C was where static was introduced) to show static and typed const are equivalent.
Typed consts have always been writable. They were first writable and later you could switch that off.
also related to equus asinus.

glorfin

  • Jr. Member
  • **
  • Posts: 66
Re: Redeclared constant as variable
« Reply #19 on: July 26, 2019, 01:44:24 am »
Writable typed constants are much older than Object Pascal.
They serve - amongst other - a distinct purpose: maintaining state within the context of a procedure or function.
example:
Code: Pascal  [Select]
  1. program state;
  2. {$mode iso} (* ISO mode has no object extensions and $J+ is the default. *)
  3. function HowManyTimesDidYouCallMe:integer;
  4. const T: integer = 0;
  5. begin
  6.   inc(T);
  7.   HowManyTimesDidYouCallMe := T;
  8. end;
  9.  
  10. var i:integer;
  11. begin
  12.   for i := 0 to 9 do writeln(HowManyTimesDidYouCallMe);
  13. end.

So it is actually *very* usefull in a non-object oriented pascal program.


Well, I agree that initialized variables are useful. The only question is - why should they be called constants? In my view, it is much worse than streams where you can search back and force. So, I think, modern syntax

var
  I:integer = 5;

is better and much more logical.

lucamar

  • Hero Member
  • *****
  • Posts: 2076
Re: Redeclared constant as variable
« Reply #20 on: July 26, 2019, 01:54:37 am »
Well, I agree that initialized variables are useful. The only question is - why should they be called constants? In my view, it is much worse than streams where you can search back and force. So, I think, modern syntax
Code: [Select]
var
  I:integer = 5;

is better and much more logical.

There is a big difference: the local const keeps its value between invocations while the var is initialized each time the function is called. This means that the constant can be used to keep internal state, which is not the case with an initialized variable.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

glorfin

  • Jr. Member
  • **
  • Posts: 66
Re: Redeclared constant as variable
« Reply #21 on: July 26, 2019, 02:04:09 am »
Hmm...
I just tested:
Code: Pascal  [Select]
  1. program Project1;
  2.  
  3. procedure MyProc;
  4. const
  5.   I:integer = 0;
  6. begin
  7.   inc(I);
  8.   writeln('I');
  9. end;
  10. var
  11.   J:integer;
  12. begin
  13.   for J := 0 to 4 do
  14.     MyProc;
  15.   readln;
  16. end.
  17.  

and it printed "1" five times. So, obviously, value was not retained.

lucamar

  • Hero Member
  • *****
  • Posts: 2076
Re: Redeclared constant as variable
« Reply #22 on: July 26, 2019, 02:08:47 am »
I was testing too :D
Code: Pascal  [Select]
  1. program ConstVar;
  2.  
  3. {$mode objfpc}{ To be able to use "Result" }
  4.  
  5. var
  6.   i: integer;
  7.  
  8. function WithConst: Integer;
  9. const
  10.   x: Integer = 0;
  11. begin
  12.   Result := x;
  13.   Inc(x);
  14. end;
  15.  
  16. function WithVar: Integer;
  17. var
  18.   x: Integer = 0;
  19. begin
  20.   Result := x;
  21.   Inc(x);
  22. end;
  23.  
  24. begin
  25.   for i := 0 to 9 do
  26.     WriteLn('Const: ', WithConst, ' - Var: ', WithVar);
  27. end.

And the result, as expected:
Const: 0 - Var: 0
Const: 1 - Var: 0
Const: 2 - Var: 0
Const: 3 - Var: 0
Const: 4 - Var: 0
Const: 5 - Var: 0
Const: 6 - Var: 0
Const: 7 - Var: 0
Const: 8 - Var: 0
Const: 9 - Var: 0
which looks like a barely contested football match ;D

BTW glorfin: your program will always print the letter "I" (not the number "1"). Replace the writeln('I'); by writeln(I); (i.e. without the quotes) and you'll see the difference.
« Last Edit: July 26, 2019, 02:19:36 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

glorfin

  • Jr. Member
  • **
  • Posts: 66
Re: Redeclared constant as variable
« Reply #23 on: July 26, 2019, 02:23:17 am »
Quod erat demonstrandum.

What is more strange,

Code: Pascal  [Select]
  1. procedure MyProc;
  2. var
  3.   I:integer = 7;
  4. begin
  5.   inc(I);
  6.   inc(L);
  7.   writeln('I',' ',L);
  8. end;
  9.  

gave:
1 8
1 9
......
1 12

"L" was global typed constant,
  const L:integer = 7
I suspected that value would be indefinite beginning from 2nd call, but it did not initialize even for the first one!
So, initialized variables or typed constants can be only global.
« Last Edit: July 26, 2019, 02:26:10 am by glorfin »

lucamar

  • Hero Member
  • *****
  • Posts: 2076
Re: Redeclared constant as variable
« Reply #24 on: July 26, 2019, 02:41:37 am »
Quod erat demonstrandum.

What is more strange,

Code: Pascal  [Select]
  1. {... code snipped ...}

"L" was global typed constant,
  const L:integer = 7
I suspected that value would be indefinite beginning from 2nd call, but it did not initialize even for the first one!
So, initialized variables or typed constants can be only global.

No, look to my example. It produces exactly the result I gave (I did a: test > result.txt and copy-pasted).

The problem, again, with your code is that you're writing the letter I each time. Try this:
Code: Pascal  [Select]
  1. procedure MyProc;
  2. var
  3.   I:integer = 7;
  4. const
  5.   L: integer = 7
  6. begin
  7.   inc(I);
  8.   inc(L);
  9.   writeln(I,' ',L);
  10. end;
but copy/paste exactly what is written; use the "Select" link above the code box, then right-click the selection, click "copy", etc. You know the drill, I'm sure :)

The result on sucessive calls should be:
Code: [Select]
8 8
8 9
8 10
...
« Last Edit: July 26, 2019, 02:44:05 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

Thaddy

  • Hero Member
  • *****
  • Posts: 9169
Re: Redeclared constant as variable
« Reply #25 on: July 26, 2019, 10:52:26 am »
Quod erat demonstrandum.

What is more strange,

Code: Pascal  [Select]
  1. procedure MyProc;
  2. var
  3.   I:integer = 7;
  4. begin
  5.   inc(I);
  6.   inc(L);
  7.   writeln('I',' ',L);
  8. end;
  9.  

gave:
1 8
1 9
......
1 12

"L" was global typed constant,
  const L:integer = 7
I suspected that value would be indefinite beginning from 2nd call, but it did not initialize even for the first one!
So, initialized variables or typed constants can be only global.

You forgot t0 define {$J+} or {$writebleconsts on}. Depending on modethis sis not always the defautl. It is in {$mode iso} and {$mode tp}
also related to equus asinus.

glorfin

  • Jr. Member
  • **
  • Posts: 66
Re: Redeclared constant as variable
« Reply #26 on: July 26, 2019, 12:48:53 pm »

No, look to my example. It produces exactly the result I gave (I did a: test > result.txt and copy-pasted).

The problem, again, with your code is that you're writing the letter I each time. Try this:
Code: Pascal  [Select]
  1. procedure MyProc;
  2. var
  3.   I:integer = 7;
  4. const
  5.   L: integer = 7
  6. begin
  7.   inc(I);
  8.   inc(L);
  9.   writeln(I,' ',L);
  10. end;
but copy/paste exactly what is written; use the "Select" link above the code box, then right-click the selection, click "copy", etc. You know the drill, I'm sure :)

The result on sucessive calls should be:
Code: [Select]
8 8
8 9
8 10
...

No, there is some misunderstanding. In my code, L was global variable; by the example I wanted to illustrate that global typed constants or initialized variables behave as intended, while local become "0" each call of the procedure.

glorfin

  • Jr. Member
  • **
  • Posts: 66
Re: Redeclared constant as variable
« Reply #27 on: July 26, 2019, 01:12:10 pm »
You forgot t0 define {$J+} or {$writebleconsts on}. Depending on modethis sis not always the defautl. It is in {$mode iso} and {$mode tp}

You are right.

Code: Pascal  [Select]
  1. program testconsts;
  2. {$mode iso}
  3. const
  4.   Glob : integer = 3;
  5. procedure TestConst;
  6. const
  7.   LConst : integer = 3;
  8. var
  9.   LVar : integer = 3;
  10. begin
  11.   writeln('Global typed constant: ',Glob);
  12.   writeln('Local typed constant: ', LConst);
  13.   writeln('Local initialized variable: ',LVar);
  14.   Inc(Glob); Inc(LConst); Inc(LVar);
  15. end;
  16. var
  17.   C:integer;
  18. begin
  19.   for C := 0 to 5 do
  20.     TestConst;
  21.   readln;
  22. end.
  23.  

produced:

Local typed constant:           3
Local initialized variable:           3
Global typed constant:           4
Local typed constant:           4
Local initialized variable:           3
Global typed constant:           5
Local typed constant:           5
Local initialized variable:           3
Global typed constant:           6
Local typed constant:           6
Local initialized variable:           3
Global typed constant:           7
Local typed constant:           7
Local initialized variable:           3
Global typed constant:           8
Local typed constant:           8
Local initialized variable:           3


Thaddy

  • Hero Member
  • *****
  • Posts: 9169
Re: Redeclared constant as variable
« Reply #28 on: July 26, 2019, 01:52:25 pm »
same goes for
Code: Pascal  [Select]
  1. {$mode delphi}{$J+}
  2. // or
  3. {$mode objfpc}{$J+}
also related to equus asinus.

Peter H

  • Jr. Member
  • **
  • Posts: 57
Re: Redeclared constant as variable
« Reply #29 on: July 26, 2019, 02:19:46 pm »
This happens in C++ on windows if you forcibly write to a constant in static memory.
Which must be forced by type-casting otherwise the compiler barfs.
These are physically write protected by the OS-memory management.

So, Pascal typed constants are not really constant. ;D
« Last Edit: July 26, 2019, 02:30:09 pm by Peter H »