About global properties lazarus / 62-General ---------------------------------------------------------------------- simone · 2021-05-24 18:00 Is it possible to declare a global property where specifiers are fields (as for properties inside classes)? From the documentation it would seem so, but the compiler fails with the following code: 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 program Project1;{$Mode ObjFPC}var  fAttr : integer;  property Attr : integer read fAttr write fAttr;beginend. Thanks. ---------------------------------------------------------------------- Soner · 2021-05-24 18:18 Yes, you can create properties outside of the class. Look at the free pascal documentation aboout properties, there is examples too. ---------------------------------------------------------------------- simone · 2021-05-24 18:28 Yes, I know this. I have read the documentation. The examples are with properties where the specifiers are getters / setters with functions / procedures. In my example I use fields and I would expect it to work, as in classes. ---------------------------------------------------------------------- Soner · 2021-05-24 18:35 When you don't want setter and getter functions, why don't you use variable, like this? 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  program Project1; {$Mode ObjFPC} var      Attr : integer; begin end.  This is the proper pascal way. Properties is only usefull, when you use also setter and getter functions. ---------------------------------------------------------------------- simone · 2021-05-24 18:45 I think you did not understand my question. ---------------------------------------------------------------------- MarkMLl · 2021-05-24 18:45 Quote from: simone on May 24, 2021, 08:28:03 pmYes, I know this. I have read the documentation. The examples are with properties where the specifiers are getters / setters with functions / procedures. In my example I use fields and I would expect it to work, as in classes. Your example shows the property referring to a /variable/, not a /field/. If you look carefully at the documentation it explicitly says that the getter/setter should be a function/procedure, TBH I don't much see the point since they have to be visible to the rest of the program and exactly the same would be the case for a variable. I don't know whether the core developers made a conscious decision that since specifying a variable would be pointless they'd not support it, or if there is a deeper reason. MarkMLl ---------------------------------------------------------------------- MarkMLl · 2021-05-24 18:47 Quote from: Soner on May 24, 2021, 08:35:42 pmThis is the proper pascal way. Having global variables is widely disparaged. TBH I think that's unfair, since they're used so heavily to represent forms by Delphi/Lazarus. MarkMLl ---------------------------------------------------------------------- simone · 2021-05-24 19:05 Quote from: MarkMLl on May 24, 2021, 08:45:38 pmQuote from: simone on May 24, 2021, 08:28:03 pmYes, I know this. I have read the documentation. The examples are with properties where the specifiers are getters / setters with functions / procedures. In my example I use fields and I would expect it to work, as in classes. Your example shows the property referring to a /variable/, not a /field/. If you look carefully at the documentation it explicitly says that the getter/setter should be a function/procedure, TBH I don't much see the point since they have to be visible to the rest of the program and exactly the same would be the case for a variable. I don't know whether the core developers made a conscious decision that since specifying a variable would be pointless they'd not support it, or if there is a deeper reason. MarkMLl Thanks Mark. In the case of properties within classes, in the specifiers we can use fields, or methods that are functions / procedures. A global property is external to a class. In this case I expect to be able to use regular procedures / functions (as actually possible) or variables, as there are no fields in this case. Otherwise what is meant by 'field' in the syntax diagram on the following page? https://freepascal.org/docs-html/current/ref/refse27.html#x58-760004.7 My question is about the supported syntax. Not about the usefulness of that construct. ---------------------------------------------------------------------- MarkMLl · 2021-05-24 19:14 Quote from: simone on May 24, 2021, 09:05:55 pmThanks Mark. In the case of properties within classes, in the specifiers we can use fields, or methods that are functions / procedures. A global property is external to a class. In this case I expect to be able to use regular procedures / functions (as actually possible) or variables, as there are no fields in this case. Otherwise what is meant by 'field' in the syntax diagram on the following page? https://freepascal.org/docs-html/current/ref/refse27.html#x58-760004.7 The behaviour of the compiler would suggest that the diagram on that page is wrong. Unless one of the core developers drops by to explain their rationale for not allowing it (and why that diagram is consistent :-) I suggest raising a bug report using Mantis. You've probably gathered that I'm not entirely happy with the situation, in part because at present there's no way to protect the global variable representing a form from being overwritten. MarkMLl ---------------------------------------------------------------------- simone · 2021-05-24 19:20 Thanks Mark. I agree with your opinion about the uselessness of having a global property built that way. Global properties don't even exist in Delphi at all. But I repeat: it was a question about syntax, not semantics. I will open a bug report. ---------------------------------------------------------------------- Soner · 2021-05-24 19:23 @simone: In Pascal variables inside of classes is called fields and fields outside of the classes is called variable. You need setter/getter only if you want to react when the variable changes. Quote from: MarkMLl on May 24, 2021, 08:47:31 pmQuote from: Soner on May 24, 2021, 08:35:42 pmThis is the proper pascal way. Having global variables is widely disparaged. TBH I think that's unfair, since they're used so heavily to represent forms by Delphi/Lazarus. MarkMLl I am using global variables a lot also inside in classes. I make variable to property when I have to react  if the variable changes. In big projects I make global variable unit to share values between modules. ---------------------------------------------------------------------- PascalDragon · 2021-05-25 11:36 Quote from: MarkMLl on May 24, 2021, 09:14:28 pmQuote from: simone on May 24, 2021, 09:05:55 pmThanks Mark. In the case of properties within classes, in the specifiers we can use fields, or methods that are functions / procedures. A global property is external to a class. In this case I expect to be able to use regular procedures / functions (as actually possible) or variables, as there are no fields in this case. Otherwise what is meant by 'field' in the syntax diagram on the following page? https://freepascal.org/docs-html/current/ref/refse27.html#x58-760004.7 The behaviour of the compiler would suggest that the diagram on that page is wrong. Unless one of the core developers drops by to explain their rationale for not allowing it (and why that diagram is consistent :-) I suggest raising a bug report using Mantis. I'd say that the syntax diagram was simply copied from the one for properties for classes and never adjusted to the reality (for example default makes no sense either). The use case for global properties is to allow variable-like access to some state (e.g. some singleton instance that is instantiated lazily)restrict the modifications that an outside user is allowed If you don't have the above requirements, then you simply don't need global properties. And these points only make sense if using variables for the getter/setter is not allowed as otherwise you'll have code like this: 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 var  i: LongInt; property Foo: LongInt read i; You now have a write protected property Foo, but some outside user can still change i which might mean for you to add checks for invalid values. Quote from: MarkMLl on May 24, 2021, 09:14:28 pmYou've probably gathered that I'm not entirely happy with the situation, in part because at present there's no way to protect the global variable representing a form from being overwritten. In those cases where the global form variable is used (namely when using Application.CreateForm) you can't use a property anyway, cause there a typeless var parameter is used for the instance. ---------------------------------------------------------------------- MarkMLl · 2021-05-25 13:25 Quote from: PascalDragon on May 25, 2021, 01:36:09 pmIn those cases where the global form variable is used (namely when using Application.CreateForm) you can't use a property anyway, cause there a typeless var parameter is used for the instance. Thanks, I was hoping you'd drop in. I agree that the illustration has probably been C&Ped without review, but the text that accompanies it is fairly unambiguous. It's obviously the compiler writers' choice as to how this behaves. But I don't see the point of having field-level protection inside objects and in particular inside forms, if the (references to) forms themselves can be trivially modified. MarkMLl ---------------------------------------------------------------------- howardpc · 2021-05-25 14:30 Quote from: MarkMLl on May 25, 2021, 03:25:05 pmI don't see the point of having field-level protection inside objects and in particular inside forms, if the (references to) forms themselves can be trivially modified. The use of global variables for forms as in Delphi and Lazarus suits 99% of simple cases. Where you need protection against trivial modification you can always do something of this sort: 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 unit Unit1; {$mode objfpc}{$H+} interface uses  Forms; type  TForm1 = class(TForm)  private   public   end;   function Form1: TForm1; implementation var  frm1: TForm1 = Nil; function Form1: TForm1;begin  if not Assigned(frm1) then    Application.CreateForm(TForm1, frm1);  Result := frm1;end; {$R *.lfm} end.If Form1 is the application's main form, you have to "autocreate" it by calling Form1 in the project's .lpr, but for other forms you don't need to call Form2, Form3 etc in the .lpr unless you want to. ---------------------------------------------------------------------- MarkMLl · 2021-05-25 16:25 Quote from: howardpc on May 25, 2021, 04:30:51 pmWhere you need protection against trivial modification you can always do something of this sort: ... If Form1 is the application's main form, you have to "autocreate" it by calling Form1 in the project's .lpr, but for other forms you don't need to call Form2, Form3 etc in the .lpr unless you want to. Yes, although when I raised a bug to query why something like that wasn't being done as routine some other issues were pointed out. https://bugs.freepascal.org/view.php?id=37717 I /still/ think it would be better if the form variable were R/O outside its own unit by default. MarkMLl ---------------------------------------------------------------------- howardpc · 2021-05-25 16:37 Quote from: MarkMLl on May 25, 2021, 06:25:39 pmI /still/ think it would be better if the form variable were R/O outside its own unit by default. It seems the lack of R/O form variables is the price you have to pay for RAD, and the possible need for form events to be able to access the form instance during creation and while loading values from the .lfm. ---------------------------------------------------------------------- MarkMLl · 2021-05-25 17:36 Quote from: howardpc on May 25, 2021, 06:37:32 pmQuote from: MarkMLl on May 25, 2021, 06:25:39 pmI /still/ think it would be better if the form variable were R/O outside its own unit by default. It seems the lack of R/O form variables is the price you have to pay for RAD, and the possible need for form events to be able to access the form instance during creation and while loading values from the .lfm. I consider it to be a heavy price, particularly since if access were via a property and functions the final write could cause an irreversible lockdown. MarkMLl ---------------------------------------------------------------------- egsuh · 2021-05-30 02:17 I haven’t read all other comments. Simply it did not work.