Recent

Author Topic: Constants in constant expressions  (Read 957 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: 4377
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.  
?
Today is tomorrow's yesterday.

Thaddy

  • Hero Member
  • *****
  • Posts: 18356
  • Here stood a man who saw the Elbe and jumped it.
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 »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

440bx

  • Hero Member
  • *****
  • Posts: 5819
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.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018