Would this be a bug, or is it intended?
I think that is a bug, because code like this should not work and throw a compile time error in J- state:
{$mode delphi}{$H+}{$J-}
type
TTestme=class
const
c_one:integer = 1;
c_two:integer = 10;
c_three = 100; // untyped works of course
c_four = 'W'; //
public
constructor create(a,b:integer);
end;
constructor TTestme.create(a,b:integer);
begin
inherited create;
c_one := a;
c_two := b;
end;
var
a:TTestme;
b:TTestme;
begin
a:=TTestme.Create(1000,10000);
a.free;
writeln(b.c_one,b.c_two);
end.
Also demo's that they really work as
class const : the values set in a, propagate to b.
I can see one reason why it can't work: {$J+/-} is a
local switch, not a global switch.....
( I also tested with {$push}{$J-}....{$pop}, same effect: writable)
A way around it is to have the consts in a strict private section and access them through read-only properties.
type
TTestme=class
strict private
const
c_one:integer = 1;
c_two:integer = 10;
public
class property one:integer read c_one;
class property two:integer read c_two;
end;
But to my horror even strict private class const fields can be written to,
even over unit boundaries!!
So apart from being always writable, the visibility specifier also has no meaning for class constants. (there is some logic to the latter, but not the former)