Forum > General

Using typed constants

(1/2) > >>

simone:
As showed in the following program, I get an error if I define a costant using an expression with typed constants:


--- 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 Project1;const  X=1;  Y=1;  Z=X+Y; //OK  A : integer=1;  B : integer=1;  C=A+B; //Error: Illegal expression  D : integer=A+B; //Error: Illegal expression beginend.
Is this an expected behavior? Thank you.

Warfley:
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  [+][-]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";}};} ---function Counter: Integer;const LocalCounter: Integer = 1;begin  Result := LocalCounter; // Return current value  LocalCounter := LocalCounter + 1; // increment for the next call to be the next valueend; WriteLn(Counter); // 1WriteLn(Counter); // 2WriteLn(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:
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).

Thaddy:

--- Quote from: 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,
--- End quote ---
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.

simone:
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.

Navigation

[0] Message Index

[#] Next page

Go to full version