Recent

Author Topic: Constants in constant expressions  (Read 354 times)

BarrOff

  • Newbie
  • Posts: 5
Constants in constant expressions
« on: December 08, 2024, 11:30:26 pm »
Hello

I just picked up Pascal/Lazarus after using Delphi more than 10 years ago.
Trying to run some samples, I stumbled across an error when using a constant in the declaration of another constant, see following mwe:

Code: Pascal  [Select][+][-]
  1. Program Example;
  2.  
  3. Const
  4.   x: Integer = 42;
  5.   y: Integer = x;
  6.  
  7. Begin
  8.   writeln('Hello');
  9. End.

I get the following output when compiling with "fpc example.pas":

Code: Text  [Select][+][-]
  1. Free Pascal Compiler version 3.2.2 [2021/07/09] for x86_64
  2. Copyright (c) 1993-2021 by Florian Klaempfl and others
  3. Target OS: Linux for x86-64
  4. Compiling example.pas
  5. example.pas(6,17) Error: Illegal expression
  6. example.pas(11) Fatal: There were 1 errors compiling module, stopping
  7. Fatal: Compilation aborted
  8. Error: /usr/bin/ppcx64 returned an error exitcode

If I replace the x in y's definition with the literal value it compiles fine.

Now I have three questions:

1. Why does this error occur when using the constant but not when using a literal?
2. How can I fix this?
3. Is there maybe another (more idiomatic?) way to achieve the same result?

TRon

  • Hero Member
  • *****
  • Posts: 3778
Re: Constants in constant expressions
« Reply #1 on: December 08, 2024, 11:35:48 pm »
1. Why does this error occur when using the constant but not when using a literal?
Because the use of a typed constants (and not a real constant).

Quote
2. How can I fix this?
Use real constants instead of typed constants.

Quote
3. Is there maybe another (more idiomatic?) way to achieve the same result?
Code: Pascal  [Select][+][-]
  1. Const
  2.   x = Integer(42);
  3.   y = integer(x);
  4.  
?
I do not have to remember anything anymore thanks to total-recall.

Thaddy

  • Hero Member
  • *****
  • Posts: 16348
  • Censorship about opinions does not belong here.
Re: Constants in constant expressions
« Reply #2 on: December 08, 2024, 11:41:34 pm »
Code: Pascal  [Select][+][-]
  1. Const
  2.   x = 42;
  3.   y = x;
This resolves to integers anyway and does not take storage.
« Last Edit: December 09, 2024, 12:02:00 am by Thaddy »
There is nothing wrong with being blunt. At a minimum it is also honest.

440bx

  • Hero Member
  • *****
  • Posts: 4889
Re: Constants in constant expressions
« Reply #3 on: December 08, 2024, 11:41:59 pm »
Now I have three questions:

1. Why does this error occur when using the constant but not when using a literal?
2. How can I fix this?
3. Is there maybe another (more idiomatic?) way to achieve the same result?
Answers:

1. Neither x nor y are _compiler_ constants, they are stored in memory therefore they are variables.  This is the case in both FPC and Delphi.

2. Make them compiler constants by removing the type, e.g, x = 42;  (note the removal of the integer type)

3. see 2. above.

For the record, Delphi would NOT have compiled that code either.

HTH.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018