Recent

Author Topic: Using typed constants  (Read 431 times)

simone

  • Sr. Member
  • ****
  • Posts: 496
Using typed constants
« on: December 01, 2022, 01:09:31 pm »
As showed in the following program, I get an error if I define a costant using an expression with typed constants:

Code: Pascal  [Select][+][-]
  1. program Project1;
  2. const
  3.   X=1;
  4.   Y=1;
  5.   Z=X+Y; //OK
  6.   A : integer=1;
  7.   B : integer=1;
  8.   C=A+B; //Error: Illegal expression
  9.   D : integer=A+B; //Error: Illegal expression
  10.  
  11. begin
  12. end.

Is this an expected behavior? Thank you.
Microsoft Windows 10 64 bit - Lazarus 2.2.4

Warfley

  • Hero Member
  • *****
  • Posts: 973
Re: Using typed constants
« Reply #1 on: December 01, 2022, 01:14:13 pm »
By default typed constants are not constants but so called "writable consts" (lovely oxymoron). Technically they are static lifetime local variables, which means variables which are only locally in scope but globally in lifetime and therefore pesist between calls of the same function:
Code: Pascal  [Select][+][-]
  1. function Counter: Integer;
  2. const LocalCounter: Integer = 1;
  3. begin
  4.   Result := LocalCounter; // Return current value
  5.   LocalCounter := LocalCounter + 1; // increment for the next call to be the next value
  6. end;
  7.  
  8. WriteLn(Counter); // 1
  9. WriteLn(Counter); // 2
  10. WriteLn(Counter); // 3

From this perspective, it's quite clear why this does not work, because they are not constants, but variables and therefore their values are dynamic.

simone

  • Sr. Member
  • ****
  • Posts: 496
Re: Using typed constants
« Reply #2 on: December 01, 2022, 01:24:28 pm »
Yes, I know this oxymoron, but I expected that they could be used as constants (although writable), in expressions to define other constants (writable or not).
Microsoft Windows 10 64 bit - Lazarus 2.2.4

Thaddy

  • Hero Member
  • *****
  • Posts: 12567
Re: Using typed constants
« Reply #3 on: December 01, 2022, 01:32:09 pm »
By default typed constants are not constants but so called "writable consts" (lovely oxymoron). Technically they are static lifetime local variables,
That is not true. Typed constants just have a memory location that holds a value, whereas the compiler exchanges untyped constants with a direct value and do not have a memory location.
« Last Edit: December 01, 2022, 02:03:09 pm by Thaddy »
Writing code in an overly complex way is usually merely a complete misunderstanding of the goal you are trying to achieve.

simone

  • Sr. Member
  • ****
  • Posts: 496
Re: Using typed constants
« Reply #4 on: December 01, 2022, 01:49:43 pm »
Since they are implemented as explained by Thaddy, the value of typed constants is not known at compile time and therefore the compiler cannot assign a value to another constant that is defined using expressions that involve them.
« Last Edit: December 01, 2022, 01:53:41 pm by simone »
Microsoft Windows 10 64 bit - Lazarus 2.2.4

Edison J. B.

  • New Member
  • *
  • Posts: 10
Re: Using typed constants
« Reply #5 on: December 01, 2022, 02:14:00 pm »
By default typed constants are not constants but so called "writable consts" (lovely oxymoron). Technically they are static lifetime local variables, which means variables which are only locally in scope but globally in lifetime and therefore pesist between calls of the same function:
Code: Pascal  [Select][+][-]
  1. function Counter: Integer;
  2. const LocalCounter: Integer = 1;
  3. begin
  4.   Result := LocalCounter; // Return current value
  5.   LocalCounter := LocalCounter + 1; // increment for the next call to be the next value
  6. end;
  7.  
  8. WriteLn(Counter); // 1
  9. WriteLn(Counter); // 2
  10. WriteLn(Counter); // 3

From this perspective, it's quite clear why this does not work, because they are not constants, but variables and therefore their values are dynamic.

Greetings,

I'm porting my programs made in Delphi 7 to Lazarus and, to my surprise, assigning a value to a constant in Delphi 7 is not possible, as you put it above. In Delphi 7 this generates an error during compilation. I really don't like the idea of being able to change the value of a constant. Is it possible to prevent this type of alteration?

simone

  • Sr. Member
  • ****
  • Posts: 496
Microsoft Windows 10 64 bit - Lazarus 2.2.4

Edison J. B.

  • New Member
  • *
  • Posts: 10
Re: Using typed constants
« Reply #7 on: December 01, 2022, 02:36:34 pm »
Thank you very much. Now that I understand the difference, this greatly increases the power of the language, since I can now keep in memory the last value received by the constant. I greatly appreciate the help.

PascalDragon

  • Hero Member
  • *****
  • Posts: 4872
  • Compiler Developer
Re: Using typed constants
« Reply #8 on: December 01, 2022, 10:03:37 pm »
I'm porting my programs made in Delphi 7 to Lazarus and, to my surprise, assigning a value to a constant in Delphi 7 is not possible, as you put it above. In Delphi 7 this generates an error during compilation. I really don't like the idea of being able to change the value of a constant. Is it possible to prevent this type of alteration?

Delphi by default disables $WriteableConsts while FPC does not.

 

TinyPortal © 2005-2018