Forum > Beginners

[SOLVED] class constant ain't recognized as constant expression

(1/1)

Kays:
Why doesn't the following compile?
--- 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 cc; {$mode objfpc} type    TLala = class        public            const                x: char = 'x';            class procedure doBlub(const i: char);    end; class procedure TLala.doBlub(const i: char);begin    case i of        TLala.x:            writeln('XXX');        else            writeln(stderr, 'unacceptable input');    end;end; begin    TLala.doBlub('o');end.
--- Code: ---Free Pascal Compiler version 3.0.0 [2015/12/05] for x86_64
Copyright (c) 1993-2015 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling cc.pas
cc.pas(16,10) Error: Constant Expression expected
cc.pas(26) Fatal: There were 1 errors compiling module, stopping
Fatal: Compilation aborted
Error: /usr/bin/ppcx64 returned an error exitcode
--- End code ---
Hint: The answer is not „because you haven't wrote a constant expression there“ („42“ neither).

Martin_fr:
try

--- 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 = char('x'); 
search for "typed constants" do learn why they are not constant (same outside class)

Kays:
I do have set

--- 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-}in my original source, which is in the long form
--- 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";}};} ---{$writeableconst off}.

I forgot it in my minimal non working example.

Why does it still not work? Or to what were you referring to?

Leledumbo:

--- Quote from: Kays on April 07, 2016, 05:41:37 am ---I do have set

--- 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-}in my original source, which is in the long form
--- 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";}};} ---{$writeableconst off}.

I forgot it in my minimal non working example.

Why does it still not work? Or to what were you referring to?

--- End quote ---
That doesn't make writableconst to have const semantics, i.e.: it's NOT a constant expression as Martin_fr describe. It still has location in memory instead of immediate value.

Thaddy:
That is correct.
The following code will be translated to a char literal and will work as expected:
--- 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 = 'x' ;
In that case the compiler will generate an immediate value.

Note that writable typed consts are a means to preserve state over multiple calls, and not really consts as explained above. A real const will if possible be translated into an immediate value.

Example:
The - local - test const will be incremented over multiple calls in $J+ mode. Something that can not be achieved if it was a var:

--- 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 x;{$APPTYPE CONSOLE}{$mode objfpc}{$J+}  function myconst:integer;  const test:integer = 0;  begin    inc(test);    result := test;  end;var  i:integer;begin for i := 0 to 9 do   writeln(myconst); readln;end. 

Navigation

[0] Message Index

Go to full version