Properties with different access rights lazarus / 21-General ---------------------------------------------------------------------- MarkMLl · 2023-11-18 20:38 I have a class with a single widely-visible instance. This contains a field which, for historical reasons, is called thisIAR rather than the normal fIAR. I have subsequently added a public read-only property IAR which allows anything which can see the instance to read the field thisIAR efficiently. I would like to add a facility to allow the field to be written which (depending on compilation etc. options) contains debugging code. This is to be strictly private to the instance. Is it possible to have two IAR properties, a public readable one and a strictly private writable one? Or do I either have to define a subclass, or only call an accessor procedure directly for writing? MarkMLl ---------------------------------------------------------------------- jamie · 2023-11-18 22:58 Inline compiler statement to define the write ? Property xXXX:integer read AValueOfSomething {$IfDEF DEbug Write AValueOfSomething}{$IFEND}; I wanted to add to this if it isn't obvious, with the test you can use an INCLUDE file containing all the test code, that way the file does not to be around afterwards so this above would just confuse someone but not give them any resources to act on it. ---------------------------------------------------------------------- MarkMLl · 2023-11-19 08:22 Quote from: jamie on November 18, 2023, 11:58:21 pmInline compiler statement to define the write ? Property xXXX:integer read AValueOfSomething {$IfDEF DEbug Write AValueOfSomething}{$IFEND}; I wanted to add to this if it isn't obvious, with the test you can use an INCLUDE file containing all the test code, that way the file does not to be around afterwards so this above would just confuse someone but not give them any resources to act on it. That would prevent me from /ever/ writing to it, which would hardly be helpful. What I want is something like (untested code follows) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ...strict private  fIAR: qword;  procedure putIAR(q; qword); inline;  property IAR: qword write putIAR;public  property IAR: qword read fIAR;...procedure cc.putIAR(q; qword);begin  fIAR := q;{$ifdef LOGICANALYZER__ }  LOGICANALYZER__.State('CC', '', RegIAR, q, 48){$endif                                   }end;  i.e. all writes to the IAR register go via the setter, which will write a VCD trace if that code has been linked in (in practice, loaded from a shared library). If the compiler's doing its job properly the setter will be inlined, and will be as efficient as a direct access if debugging code is undefined. MarkMLl ---------------------------------------------------------------------- PascalDragon · 2023-11-19 17:06 Quote from: MarkMLl on November 18, 2023, 09:38:08 pmIs it possible to have two IAR properties, a public readable one and a strictly private writable one? No, it's not. At least not with the same name. Oxygene has such a syntax and I've played with the thought of adding to FPC as well, but it's not very high on my priority list. Quote from: MarkMLl on November 18, 2023, 09:38:08 pmOr do I either have to define a subclass, or only call an accessor procedure directly for writing? I usually do the later. ---------------------------------------------------------------------- KodeZwerg · 2023-11-19 17:21 Quote from: MarkMLl on November 18, 2023, 09:38:08 pmIs it possible to have two IAR properties, a public readable one and a strictly private writable one?Isnt that the default behavior of class fields and property read without write? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TSomeClass = class    strict private      FSomething: Integer;    public      constructor Create;    published      property Something: Integer read FSomething default 0; // read from it  end; ....constructor TSomeClass.Create;begin  FSomething := 123; // write to itend;So overwrite you just can do within your class, if you expose a method for write access belongs to you. ---------------------------------------------------------------------- MarkMLl · 2023-11-19 17:53 Quote from: PascalDragon on November 19, 2023, 06:06:53 pmQuote from: MarkMLl on November 18, 2023, 09:38:08 pmIs it possible to have two IAR properties, a public readable one and a strictly private writable one? No, it's not. At least not with the same name. Oxygene has such a syntax and I've played with the thought of adding to FPC as well, but it's not very high on my priority list. Quote from: MarkMLl on November 18, 2023, 09:38:08 pmOr do I either have to define a subclass, or only call an accessor procedure directly for writing? I usually do the later. Thanks for that. I anticipate using the exercise to get rid of the old "this" field prefix, which came about since at one time the code was written in JavaScript. I'd add that setters/getters is probably the area where people experience the most grief when the compiler refuses to inline. I can probably do a fair amount by bracketing the debugging code with conditional compilation directives, but there's still a risk of a substantial performance hit in frequently-executed code. MarkMLl