Lazarus

Free Pascal => General => Topic started by: simone on December 01, 2022, 01:09:31 pm

Title: Using typed constants
Post by: simone 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.
Title: Re: Using typed constants
Post by: Warfley 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.
Title: Re: Using typed constants
Post by: simone 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).
Title: Re: Using typed constants
Post by: Thaddy 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.
Title: Re: Using typed constants
Post by: simone 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.
Title: Re: Using typed constants
Post by: Edison J. B. 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?
Title: Re: Using typed constants
Post by: simone on December 01, 2022, 02:21:42 pm
See: https://www.freepascal.org/docs-html/current/prog/progsu42.html#x49-480001.2.42
Title: Re: Using typed constants
Post by: Edison J. B. 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.
Title: Re: Using typed constants
Post by: PascalDragon 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