Take a look at the code:

Take a look at the code:

program ConstantExpr;
{$APPTYPE CONSOLE}
const c1: Char = 'B';
var c: Char;
begin
c := c1;
// c1 := 'Z'; E2064 Left side cannot be assigned to -- it's OK
case c of
'A': ;
c1: ; // E2026 Constant expression expected ???
end;
end.

The compiler has decided that c1 is not a constant, although it is certainly a constant. This is probably a bug in the compiler.

Update: OK, I have a workaround for this case. Change the constant declaration as follows:
const c1 = Char('B');
After that the code compiles fine.
Anyway I think it's a compiler's bug, do you?

Comments

  1. David Heffernan Yes, it's documented as Dalija Prasnikar pointed out above. Yet I think the use of typed constants can be expanded including the case statement.

    ReplyDelete
  2. T n T The documentation that is most important is that for the case statement

    ReplyDelete
  3. It's a feature. The use case making their use clearer is to initialise variables during declaration: http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Variables_(Delphi) However, they cannot be declared in local scope, unlike typed constants: http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Declared_Constants
    docwiki.embarcadero.com - Variables (Delphi) - RAD Studio

    ReplyDelete

Post a Comment