Forum > Beginners

Constants in constant expressions

(1/1)

BarrOff:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Program Example; Const  x: Integer = 42;  y: Integer = x; Begin  writeln('Hello');End.
I get the following output when compiling with "fpc example.pas":


--- Code: Text  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Free Pascal Compiler version 3.2.2 [2021/07/09] for x86_64Copyright (c) 1993-2021 by Florian Klaempfl and othersTarget OS: Linux for x86-64Compiling example.pasexample.pas(6,17) Error: Illegal expressionexample.pas(11) Fatal: There were 1 errors compiling module, stoppingFatal: Compilation abortedError: /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:

--- Quote from: BarrOff on December 08, 2024, 11:30:26 pm ---1. Why does this error occur when using the constant but not when using a literal?

--- End quote ---
Because the use of a typed constants (and not a real constant).


--- Quote ---2. How can I fix this?

--- End quote ---
Use real constants instead of typed constants.


--- Quote ---3. Is there maybe another (more idiomatic?) way to achieve the same result?

--- End quote ---

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Const  x = Integer(42);  y = integer(x); ?

Thaddy:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Const  x = 42;  y = x;This resolves to integers anyway and does not take storage.

440bx:

--- Quote from: BarrOff on December 08, 2024, 11:30:26 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?

--- End quote ---
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.

Navigation

[0] Message Index

Go to full version