mvanrijnen 123 Posted July 20, 2021 (edited) Hey guys, i'm trying to implement something with attributes, which for the most works, but stumble on some problems with something. I have a setting class, which i use with class decoration, following works just fine: type TMyClass = class private fmyint : integer; public [Setting(666), SettingGroup('MySettingGroup1')] property MyInt : integer read FMyInt write FMyInt; end; So the property MyInt has a default value of 666, and through a SettingWorker class i can store and retrieve settings from json, ini's, database or whatever, that works just fine. (the SettingWorker class has methods like ApplyDefaults, LoadIni, SaveIni, etc etc) so now the challenge, i want to use this for some more complex properties (records like TPoint etc, this works: type TMyClass = class private fmyPoint : TPoint; public [Setting(), SettingGroup('MySettingGroup1')] property MyPoint : TPoint read FMyPoint write FMyPoint; end; so, i can store and retrieve the MyPoint value no problem, no i want to introduce the default value for this: This does not work: const CNST_MYPOINT : TPoint = (X: 35; Y: 55); type TMyClass = class private fmyPoint : TPoint; public [Setting(CNST_MYPOINT), SettingGroup('MySettingGroup1')] property MyPoint : TPoint read FMyPoint write FMyPoint; end; (i get: E2026 Constant expression expected) This also does not work: type TMyClass = class private fmyPoint : TPoint; public [Setting(TPoint = (X: 35; Y: 55)), SettingGroup('MySettingGroup1')] property MyPoint : TPoint read FMyPoint write FMyPoint; end; (i get: E2029 '(' expected but '=' found) Any thoughts, remark, ideas ? Edited July 20, 2021 by mvanrijnen Share this post Link to post
Lars Fosdal 1792 Posted July 20, 2021 I've butted my head against this, and sadly there currently is no way to pass a typed const to an attribute. There are reports for problems with typed consts - so please vote. https://quality.embarcadero.com/browse/RSP-13921 likewise, for dynamic arrays https://quality.embarcadero.com/browse/RSP-32488 1 Share this post Link to post
David Heffernan 2345 Posted July 20, 2021 Attribute syntax demands true constants Share this post Link to post
mvanrijnen 123 Posted July 20, 2021 (edited) 1 hour ago, David Heffernan said: Attribute syntax demands true constants So we need true constants for records 🙂 Always found it strange the variable constant construction in Delphi. Try to explain it to someone: * var x : integer; Variables: something from which the value can change throughout the execution of the application. * const x = 5; Constant: something from which the value can not change throughout the execution of the application. * const X : integer = 5; A constant from which the value can change throughout the execution of the application, so whats constant on this? If i want to explain this to my mother, she be asking if everything is alright with me 🙂 (and she uses an iphone, ipad and a notebook) Edited July 20, 2021 by mvanrijnen 1 Share this post Link to post
Fr0sT.Brutal 900 Posted July 20, 2021 (edited) 13 minutes ago, mvanrijnen said: const X : integer = 5; A constant from which the value can change throughout the execution of the application, so whats constant on this? Nope. It's a constant that couldn't be changed. But it is typed. Alas, structured constants could only be defined in this form so cannot be used in expressions where "pure" constants required. From the compiler POV, true constants of simple types don't exist somewhere in code, their value is just substituted in-place. Typed constants, OTOH, are like read-only vars - they sit in memory and have their address. Edited July 20, 2021 by Fr0sT.Brutal Share this post Link to post
mvanrijnen 123 Posted July 20, 2021 (edited) Yes i'm mistaking it with something else, there is (or was) such a thing in Delphi, Have to check some very old code if i can find it. Edited July 20, 2021 by mvanrijnen Share this post Link to post
uligerhardt 18 Posted July 20, 2021 (edited) 1 hour ago, mvanrijnen said: Yes i'm mistaking it with something else, there is (or was) such a thing in Delphi, Have to check some very old code if i can find it. There is http://docwiki.embarcadero.com/RADStudio/Sydney/en/Writeable_typed_constants_(Delphi). It's just off per default for some years now. Edited July 20, 2021 by uligerhardt Share this post Link to post
mvanrijnen 123 Posted July 20, 2021 ah ok, totally missed on that 🙂 sorry for the confusion Share this post Link to post
Fr0sT.Brutal 900 Posted July 20, 2021 Well, any typed const could be modified by pointer 2 hours ago, mvanrijnen said: Yes i'm mistaking it with something else, there is (or was) such a thing in Delphi, Have to check some very old code if i can find it. seems you recall ancient times when this was true indeed Quote In early versions of Delphi and Object Pascal, typed constants were always writeable 2 2 Share this post Link to post
David Heffernan 2345 Posted July 20, 2021 1 hour ago, Fr0sT.Brutal said: Well, any typed const could be modified by pointer Aren't they placed in read only memory? Share this post Link to post
Fr0sT.Brutal 900 Posted July 21, 2021 16 hours ago, David Heffernan said: Aren't they placed in read only memory? I'm not sure what do you mean by read-only memory but this works: const b: byte = 1; procedure TForm1.Button2Click(Sender: TObject); begin pbyte(@b)^ := 2; end; Share this post Link to post
Lars Fosdal 1792 Posted July 21, 2021 @Fr0sT.Brutal - Same for records. {$WRITEABLECONST OFF} const TypedConst: xlt = (no:'Norsk'; se: 'Svensk'; en:'English'); type pxlt = ^xlt; procedure TForm1.TestTypedConst; procedure Show(const aConst: xlt); begin Memo1.Lines.Add(aConst.no +', '+ aConst.se +', '+ aConst.en); end; begin Show(TypedConst); pxlt(@TypedConst)^.se := 'Deutsch'; Show(TypedConst); end; Output Norsk, Svensk, English Norsk, Deutsch, English 16 hours ago, David Heffernan said: Aren't they placed in read only memory? So, the answer is a definitive no. Edit: Note that with WRITEABLECONST OFF TypedConst.se := 'Deutsch'; gives a [dcc32 Error]: E2064 Left side cannot be assigned to while it compiles with WRITEABLECONST ON. Share this post Link to post
Lars Fosdal 1792 Posted July 21, 2021 We need a truly immutable typed const. Share this post Link to post
Fr0sT.Brutal 900 Posted July 21, 2021 1 hour ago, Lars Fosdal said: Same for records. Yep, sure. I use this trick for some lookup tables that must be constant but can't be defined in code. I fill them via pointer at unit init Share this post Link to post
David Heffernan 2345 Posted July 21, 2021 3 hours ago, Lars Fosdal said: 20 hours ago, David Heffernan said: Aren't they placed in read only memory? So, the answer is a definitive no. Yeah, you are right, I just assumed that typed constants would be placed in read only memory, as they are in other tool chains. Oh well. Share this post Link to post