Recent

Author Topic: [SOLVED] class constant ain't recognized as constant expression  (Read 3000 times)

Kays

  • Hero Member
  • *****
  • Posts: 624
  • Whasup!?
    • KaiBurghardt.de
Why doesn't the following compile?
Code: Pascal  [Select][+][-]
  1. program cc;
  2.  
  3. {$mode objfpc}
  4.  
  5. type
  6.     TLala = class
  7.         public
  8.             const
  9.                 x: char = 'x';
  10.             class procedure doBlub(const i: char);
  11.     end;
  12.  
  13. class procedure TLala.doBlub(const i: char);
  14. begin
  15.     case i of
  16.         TLala.x:
  17.             writeln('XXX');
  18.         else
  19.             writeln(stderr, 'unacceptable input');
  20.     end;
  21. end;
  22.  
  23. begin
  24.     TLala.doBlub('o');
  25. end.
Code: [Select]
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
Hint: The answer is not „because you haven't wrote a constant expression there“ („42“ neither).
« Last Edit: April 07, 2016, 04:47:49 pm by Kays »
Yours Sincerely
Kai Burghardt

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11800
  • Debugger - SynEdit - and more
    • wiki
Re: class constant ain't recognized as constant expression
« Reply #1 on: April 07, 2016, 05:03:19 am »
try
Code: Pascal  [Select][+][-]
  1.                  
  2. const
  3.     x = char('x');
  4.  

search for "typed constants" do learn why they are not constant (same outside class)

Kays

  • Hero Member
  • *****
  • Posts: 624
  • Whasup!?
    • KaiBurghardt.de
Re: class constant ain't recognized as constant expression
« Reply #2 on: April 07, 2016, 05:41:37 am »
I do have set
Code: Pascal  [Select][+][-]
  1. {$J-}
in my original source, which is in the long form
Code: Pascal  [Select][+][-]
  1. {$writeableconst off}
.

I forgot it in my minimal non working example.

Why does it still not work? Or to what were you referring to?
« Last Edit: April 07, 2016, 05:44:02 am by Kays »
Yours Sincerely
Kai Burghardt

Leledumbo

  • Hero Member
  • *****
  • Posts: 8831
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: class constant ain't recognized as constant expression
« Reply #3 on: April 07, 2016, 08:43:07 am »
I do have set
Code: Pascal  [Select][+][-]
  1. {$J-}
in my original source, which is in the long form
Code: Pascal  [Select][+][-]
  1. {$writeableconst off}
.

I forgot it in my minimal non working example.

Why does it still not work? Or to what were you referring to?
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

  • Hero Member
  • *****
  • Posts: 18324
  • Here stood a man who saw the Elbe and jumped it.
Re: class constant ain't recognized as constant expression
« Reply #4 on: April 07, 2016, 09:38:20 am »
That is correct.
The following code will be translated to a char literal and will work as expected:
Code: Pascal  [Select][+][-]
  1. 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  [Select][+][-]
  1. program x;
  2. {$APPTYPE CONSOLE}
  3. {$mode objfpc}{$J+}
  4.   function myconst:integer;
  5.   const test:integer = 0;
  6.   begin
  7.     inc(test);
  8.     result := test;
  9.   end;
  10. var
  11.   i:integer;
  12. begin
  13.  for i := 0 to 9 do
  14.    writeln(myconst);
  15.  readln;
  16. end.
  17.  
« Last Edit: April 07, 2016, 10:16:33 am by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

 

TinyPortal © 2005-2018