Jump to content
mvanrijnen

Const Records and Class/Property Attributes (decoration)

Recommended Posts

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 by mvanrijnen

Share this post


Link to post
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 by mvanrijnen
  • Haha 1

Share this post


Link to post
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 by Fr0sT.Brutal

Share this post


Link to post

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 by mvanrijnen

Share this post


Link to post

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

image.png.6e7551ede236bebca7043c864cc6037c.png

  • Like 2
  • Haha 2

Share this post


Link to post
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
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

@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
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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×