Forum > General

Evaluation of constant statements

<< < (2/21) > >>

Thaddy:

--- Quote from: 440bx on September 21, 2022, 04:33:51 pm ---true constants are evaluated at compile time, therefore the expression (1- TFy) doesn't exist past its declaration, what exists after its evaluation is its result.

--- End quote ---
Yes, but the precedence is equal at both compile time and runtime. 1-Tfy still relies on the same precedence even if it is already resolved at compile time. The evaluation order is the same both ways.

I interpreted his unease in that fashion, but indeed true const expressions are evaluated before compilation and replaced by a single constant. The same is true for typed constants in {$J-} mode also.

AlanTheBeast:

--- Quote from: KodeZwerg on September 21, 2022, 04:25:02 pm ---
--- Quote from: AlanTheBeast on September 21, 2022, 04:20:26 pm ---
--- 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  TFy = 0.95;  TFx = 1 - TFy;
--- 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  TFy = Double(0.95);  TFx = Double(1 - TFy);Thats how I would write such to have a proper type set.

--- End quote ---

Well, I did have

--- 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";}};} ---    TFy: Single = 0.95;   Tfx: Single = 1 - TFy; But that failed to compile.

But your solution works fine for me.  Never thought of a type cast in a constant statement.

Thanks.

AlanTheBeast:

--- Quote from: 440bx on September 21, 2022, 04:33:51 pm ---
--- Quote from: AlanTheBeast on September 21, 2022, 04:20:26 pm ---I don't get that warm and fuzzy feeling that (1-TFy) is operated separately before (or after) the multiplication.

--- End quote ---
true constants are evaluated at compile time, therefore the expression (1- TFy) doesn't exist past its declaration, what exists after its evaluation is its result.

--- End quote ---

What bothers me is that if I do a nice simple integer const set up such as:


--- 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  A = 43;  B = 72-A;
There is no compile time error.  I take the B as pre-computed and snug as a bug in rug.

Yet,

--- 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    TFy: Single = 0.95;   Tfx: Single = 1 - TFy;
Fails to achieve snug buggness.

IAC, KodeZwerg's solution is fine for my needs.

Thanks.

Thaddy:
Indeed, I made a mistake. Type constants will throw an error.

Arioch:

--- 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";}};} ---{$J-} const A: integer = 10;const B = A + 2;  // unit1.pas(87,1) Error: Illegal expression 
indeed, this compiles neither in Delphi nor in FPC.

Frankly, to me it looks like compiler defficiency. A bug wit hpriority too low to ever get fixed.

Unless they tacitly encourage hacks like


--- 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";}};} ---{$J-} const A: integer = 10;var B: PInteger;begin  B := @A; B^ := 20; 

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version