Making variables read-only everywhere, but read/write in only one unit. lazarus / 21-General ---------------------------------------------------------------------- Curt Carpenter · 2022-09-04 13:05 I have a group of variables which I want to be available for both reading and writing in one unit, but available read-only in all the other units in my program.  Is there a formal way to accomplish this in fpc?  Currently, I've been putting variables that I want to be available everywhere in my program in a separate "globals" unit, then letting all units "use" globals.  But of course this makes those variables available for both reading and writing everywhere, which can be a problem for undisciplined programmers (me) working on a large program with a lot of units.  Insights appreciated. ---------------------------------------------------------------------- jamie · 2022-09-04 13:11 Yes, make the variables in the read write unit declared in the Implementation section and supply functions with their headers in the Interface section.  Write the code for those functions in the implementation section.  make them inline for better code generation. ---------------------------------------------------------------------- Zvoni · 2022-09-04 13:15 The WritableConst compiler directive? ---------------------------------------------------------------------- jamie · 2022-09-04 13:26 No, writeable has nothing to do with it. Poster wants to make editable variables locally to one unit but make them read only to others, for that you need a function access point for the external references. an inline function, if the compiler is working correctly, should provide a simple direct read of the variable but not allow it to be written to via external units. ---------------------------------------------------------------------- egsuh · 2022-09-04 13:30 Use property. That’s one of what property intends to do. ---------------------------------------------------------------------- jamie · 2022-09-04 13:34 Yes, a property would also work for that. Something not very often used in the interface section but that would, also cause issues locally if you restrict the write access. Then the local unit can't write to it.  Oh Well, more than one way to skin a cat, I guess. EDIT   You can also create a CLass locally that marks the properties as READ only but the local unit will have inner access to the actual variable as in protected. But that is just generating more code than needed. ---------------------------------------------------------------------- Kays · 2022-09-04 13:52 Quote from: jamie on September 04, 2022, 03:11:47 pm[…] make them inline for better code generation.{$optimization autoInline} should do that for us. Quote from: jamie on September 04, 2022, 03:34:05 pmYes, a property would also work for that.You will still need a getter function. I first thought “that must be solution”, too, but something like 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        actualX: integer;property x: integer read actualX;does not work, because a “class field or method identifier” is expected, which actualX is not. ---------------------------------------------------------------------- KodeZwerg · 2022-09-04 14:07 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 {$modeswitch advancedrecords} type  TTest = record    private      FReadOnly: string;    public      property ReadMe: string read FReadOnly;  end; var  Test: TTest;begin  Test.FReadOnly := 'Test'; // only possible inside unit with type definition  //Test.ReadMe := 'impossible';  WriteLn(Test.ReadMe);  ReadLn;end. ---------------------------------------------------------------------- MarkMLl · 2022-09-04 14:33 Quote from: KodeZwerg on September 04, 2022, 04:07:14 pmCode: [Select]  Test.FReadOnly := 'Test'; // only possible inside unit with type definition But that doesn't stop the variable Test from being overwritten. Incidentally, we see « Last Edit: Tomorrow at 31:76:97 by KodeZwerg »... what timezone are you in? MarkMLl ---------------------------------------------------------------------- Curt Carpenter · 2022-09-04 15:57 Appreciate the inputs. I was hoping for a way to restrict making assignments to an otherwise global variable that would not impose any overhead on the "read" operation (such as the need for an access function).  Perhaps something that would just cause the compiler to issue a warning if I tried to assign to a variable that was "write restricted" to it's declaring unit.  (Good programming discipline would make such a thing unnecessary of course, and that may be why it's not already handled in the compiler and why I wish it was!) ---------------------------------------------------------------------- KodeZwerg · 2022-09-04 16:33 Quote from: MarkMLl on September 04, 2022, 04:33:26 pmBut that doesn't stop the variable Test from being overwritten. Overwritten with what? It can be assigned and have for each assignment the same value "ReadMe" that the unit where it is defined has set. I did thought that was the intention, have some value readable from other units but writeable in the unit where it come from. Maybe add a constructor to call to initialize the basic values... ---------------------------------------------------------------------- jamie · 2022-09-04 16:47 hmm, here is a working concept. The only issue here is the property name can't be the same as the internal name but that shouldn't be an issue. 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  Classes, SysUtils, Forms, Controls, Graphics, Dialogs; type  TForm1 = class(TForm)  private   public   end;TMyVariables = Object  Private  // Make then accessable from the unit that houses them.  V1,V2,V3,V4:Integer;  Public  Property Read_V1:Integer Read V1; //Make then Read only to anywhere.  property Read_V2:Integer Read V2;  Property Read_V3:Integer Read V3;  Property Read_V4:Integer Read V4; End;var  Form1: TForm1;  MyVariables:TMyVariables;implementation {$R *.lfm} // access all of your variables in what ever way you wish.end.   In this manner, external references to this object will not allow access to the writable variables but only will see the properties, which are read only. ---------------------------------------------------------------------- Curt Carpenter · 2022-09-04 17:32 I've thought about that approach jamie, and it would achieve the desired effect.  But suspect that it imposes run-time overhead associated with accessing the object properties that really isn't necessary.   I was hoping for a property that could be assigned to any variable when it is declared, making it read/write within the unit of declaration but read-only elsewhere (with the restriction checked only at compile time, so no run time overhead). ---------------------------------------------------------------------- PascalDragon · 2022-09-04 18:09 Quote from: Curt Carpenter on September 04, 2022, 07:32:47 pmI've thought about that approach jamie, and it would achieve the desired effect.  But suspect that it imposes run-time overhead associated with accessing the object properties that really isn't necessary.   I was hoping for a property that could be assigned to any variable when it is declared, making it read/write within the unit of declaration but read-only elsewhere (with the restriction checked only at compile time, so no run time overhead). The suggestion by jamie is among those with the lowest overhead that also works with 3.2.0. In 3.3.1 I'd suggest the property with a getter declared as inline, because there inlining would work correctly with a variable declared in the implementation section as well. ---------------------------------------------------------------------- MarkMLl · 2022-09-04 18:55 Quote from: PascalDragon on September 04, 2022, 08:09:10 pmThe suggestion by jamie is among those with the lowest overhead that also works with 3.2.0. In 3.3.1 I'd suggest the property with a getter declared as inline, because there inlining would work correctly with a variable declared in the implementation section as well. I think that the only way it can be done is to have a property at the unit level with exported inlined getter: going via an object still leaves the object vulnerable and has scope for an initialisation/access ambiguity. It's also possible to have a setter which is either private to the unit, or is published with e.g. the condition that it may also be called once or must be passed a special access token. I wish there was a better way, particularly since there are "old hands" intent on lecturing the rest of us that globals are invariably a Bad Thing, despite the fact that Lazarus and Delphi use unprotected globals to represent forms. MarkMLl ---------------------------------------------------------------------- PascalDragon · 2022-09-04 19:05 Quote from: MarkMLl on September 04, 2022, 08:55:40 pmQuote from: PascalDragon on September 04, 2022, 08:09:10 pmThe suggestion by jamie is among those with the lowest overhead that also works with 3.2.0. In 3.3.1 I'd suggest the property with a getter declared as inline, because there inlining would work correctly with a variable declared in the implementation section as well. I think that the only way it can be done is to have a property at the unit level with exported inlined getter: going via an object still leaves the object vulnerable and has scope for an initialisation/access ambiguity. It's also possible to have a setter which is either private to the unit, or is published with e.g. the condition that it may also be called once or must be passed a special access token. Right, forgot about that part. ---------------------------------------------------------------------- MarkMLl · 2022-09-04 19:14 Quote from: PascalDragon on September 04, 2022, 09:05:03 pmQuote from: MarkMLl on September 04, 2022, 08:55:40 pm...the condition that it may also be called once or must be passed a special access token. Right, forgot about that part. I think that should read may /only/ be called once. In practice I'm not sure how you'd pass an access token to a setter associated with a property, unless it were something implicit like the address of the caller (can this be done portably?). MarkMLl ---------------------------------------------------------------------- ASerge · 2022-09-04 19:46 Quote from: Curt Carpenter on September 04, 2022, 07:32:47 pmBut suspect that it imposes run-time overhead associated with accessing the object properties that really isn't necessary. Practice does not confirm suspect. 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} interface type  TReadOnly = class  private    class var FVariable1: Integer;  public    class property Variable1: Integer read FVariable1;  end; implementation type  TReadWrite = class helper for TReadOnly    class procedure SetVariable1(const Value: Integer); static inline;    class property Variable1: Integer write SetVariable1;  end; class procedure TReadWrite.SetVariable1(const Value: Integer); static;begin  FVariable1 := Value;end;  initialization  TReadOnly.Variable1 := 2; // Write accessend. 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 {$APPTYPE CONSOLE}{$MODE OBJFPC} uses unit1; begin  Writeln(TReadOnly.Variable1);  Readln;end. Asm codeCode: ASM  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i # [7] Writeln(TReadOnly.Variable1);        call    fpc_get_output        movq    %rax,%rbx        movslq  U_$UNIT1_$$__static_treadonly_FVARIABLE1(%rip),%r8   # direct access!        movq    %rbx,%rdx        ... Code: ASM  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i # [30] TReadOnly.Variable1 := 2; // Write access        movl    $2,U_$UNIT1_$$__static_treadonly_FVARIABLE1(%rip) # direct access also ---------------------------------------------------------------------- egsuh · 2022-09-05 02:56 Delphi doesn't, but Free Pascal supports global properties. I made a simple test project. 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 that contains variable fieldsunit uField;{$mode ObjFPC}{$H+}interfaceuses  Classes, SysUtils;var  FData: integer;implementationend. //------------------ unit that defines Read/Write methods for propertiesunit uPropertyRW;{$mode ObjFPC}{$H+}interfaceuses  Classes, SysUtils, uField; function getData: integer;procedure setData (AValue:integer);property Data: integer read getData write setData; implementationfunction getData: integer;begin   Result := FData;end;procedure setData(AValue: integer);begin   FData := AValue;end;end.  //------------------ unit that defines Read-only methods for propertiesunit uReadOnly;{$mode ObjFPC}{$H+}interfaceuses  Classes, SysUtils, uField; function getData: integer;property Data: integer read getData; implementationfunction getData: integer;begin   Result := FData;end;end. //------------------ unit using Read/Write propertiesunit UnitA;{$mode objfpc}{$H+}interfaceuses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type  TForm1 = class(TForm)    Button1: TButton;    Button2: TButton;    Button3: TButton;    Edit1: TEdit;    procedure Button1Click(Sender: TObject);    procedure Button2Click(Sender: TObject);    procedure Button3Click(Sender: TObject);  end; var  Form1: TForm1; implementation uses uPropertyRW, unitB; {$R *.lfm} procedure TForm1.Button1Click(Sender: TObject);begin    Data := StrToIntDef(Edit1.Text, -99999);end; procedure TForm1.Button2Click(Sender: TObject);begin   ShowMessage(IntToStr(Data));end; procedure TForm1.Button3Click(Sender: TObject);begin   Form2.Show;end;end. //------------------ unit using Read-only propertiesunit unitB;{$mode ObjFPC}{$H+}interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type  TForm2 = class(TForm)    Button1: TButton;    procedure Button1Click(Sender: TObject);  end; var  Form2: TForm2; implementation uses uReadOnly; {$R *.lfm} procedure TForm2.Button1Click(Sender: TObject);begin    // Data := 0;   // this causes compilation error.     ShowMessage(IntToStr(Data));end;end.  This increases the amount of typing, but once written then no more. ---------------------------------------------------------------------- KodeZwerg · 2022-09-05 04:42 Test project: 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; uses unit1; var  Test: TReadOnly;begin  Test := TReadOnly.Create;  try    WriteLn(Test.BooleanValue);    WriteLn(Test.StringValue);    WriteLn(Test.IntegerValue);  finally    Test.Free;  end;  ReadLn;end.  Only in this unit you can read and write, in all others it is read only. 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  Classes, SysUtils; type  TReadOnly = class(TObject)    strict private      FBoolean: Boolean;    FString: string;      FInteger: Integer;    public      constructor Create;    public      property BooleanValue: Boolean read FBoolean;    property StringValue: string read FString;      property IntegerValue: Integer read FInteger;  end;  implementation constructor TReadOnly.Create;begin  inherited Create;  FBoolean := True;  FString := 'I am read only.';  FInteger := 666;end; end. ---------------------------------------------------------------------- MarkMLl · 2022-09-05 06:47 Quote from: egsuh on September 05, 2022, 04:56:11 amThis increases the amount of typing, but once written then no more. And what prevents an errant program importing uField and using the content directly? MarkMLl ---------------------------------------------------------------------- MarkMLl · 2022-09-05 06:50 Quote from: KodeZwerg on September 05, 2022, 06:42:34 amOnly in this unit you can read and write, in all others it is read only. Except that the variable Test is completely unprotected. MarkMLl ---------------------------------------------------------------------- serbod · 2022-09-05 10:23 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 interface function GlobalUserName(): string; // available globally, read-only implementation var  LocalUserName: string; // read-write locally function GlobalUserName(): string;begin  Result := LocalUserName;end; ---------------------------------------------------------------------- MarkMLl · 2022-09-05 10:53 @Serbod: please read the existing discussion before commenting. OP has specifically asked about a variable and has specifically said that he is concerned about the overhead of using a function either directly or as the accessor of a property. PascalDragon has specifically commented on the (lack of) associated overhead at least in the development version of the compiler, and ASerge has gone to the trouble of looking at the generated assembler. MarkMLl ---------------------------------------------------------------------- KodeZwerg · 2022-09-05 11:09 Quote from: MarkMLl on September 05, 2022, 08:50:52 amQuote from: KodeZwerg on September 05, 2022, 06:42:34 amOnly in this unit you can read and write, in all others it is read only. Except that the variable Test is completely unprotected. MarkMLl I do not fully understand, can you show an example of your statement for a better understanding please? In my variant the wanted variables are readable from everywhere via properties but writeable only in its class unit via field name. ---------------------------------------------------------------------- MarkMLl · 2022-09-05 11:22 Quote from: KodeZwerg on September 05, 2022, 01:09:32 pmI do not fully understand, can you show an example of your statement for a better understanding please? In my variant the wanted variables are readable from everywhere via properties but writeable only in its class unit via field name. Code: [Select]program Project1; uses unit1; var   Test: TReadOnly; begin   Test := TReadOnly.Create;   Test := nil end. The test variable is unprotected. The fields it contains are protected. The only viable answer (as first pointed out in this thread by Jamie, but it is rather an FAQ) is to use a property with an accessor function. PascalDragon has added to that that inline will eliminate overhead as of FPC 3.3.1. I'd add that it's not possible to use a property referring directly to a variable, since that would force the variable to similarly be in the interface part hence exposed. Granted that PascalDragon has pointed out that inline makes the accessor cheap, but I think there might be an argument for a forward qualifier here which would allow the variable to be in the implementation part of the unit. MarkMLl ---------------------------------------------------------------------- PascalDragon · 2022-09-05 11:34 Quote from: MarkMLl on September 05, 2022, 01:22:22 pmI'd add that it's not possible to use a property referring directly to a variable, since that would force the variable to similarly be in the interface part hence exposed. Granted that PascalDragon has pointed out that inline makes the accessor cheap, but I think there might be an argument for a forward qualifier here which would allow the variable to be in the implementation part of the unit. A forward declaration in that sense is already possible by using external, however then it obviously won't be write protected. We won't introduce anything else here, because this would be a whole new thing to only solve a problem which in my personal opinion isn't even a problem that needs to be dealt with. ---------------------------------------------------------------------- MarkMLl · 2022-09-05 17:36 Quote from: PascalDragon on September 05, 2022, 01:34:45 pmWe won't introduce anything else here, because this would be a whole new thing to only solve a problem which in my personal opinion isn't even a problem that needs to be dealt with. I'm afraid that this is something on which we'll have to disagree. Being able to restrict accessibility is a fundamental issue of language robustness, and its implementation is far too important to be left to the vagaries of the compiler (specifically, whether it deigns to honour the inline directive). MarkMLl ---------------------------------------------------------------------- KodeZwerg · 2022-09-05 20:43 Quote from: MarkMLl on September 05, 2022, 01:22:22 pmQuote from: KodeZwerg on September 05, 2022, 01:09:32 pmI do not fully understand, can you show an example of your statement for a better understanding please? In my variant the wanted variables are readable from everywhere via properties but writeable only in its class unit via field name. Code: [Select]program Project1; uses unit1; var   Test: TReadOnly; begin   Test := TReadOnly.Create;   Test := nil end. The test variable is unprotected. The fields it contains are protected. The only viable answer (as first pointed out in this thread by Jamie, but it is rather an FAQ) is to use a property with an accessor function. PascalDragon has added to that that inline will eliminate overhead as of FPC 3.3.1. I'd add that it's not possible to use a property referring directly to a variable, since that would force the variable to similarly be in the interface part hence exposed. Granted that PascalDragon has pointed out that inline makes the accessor cheap, but I think there might be an argument for a forward qualifier here which would allow the variable to be in the implementation part of the unit. MarkMLl It is like you tell me, i exclude the unit where the data comes from, or i overwrite the access to methods that need one of those "read- only" values. It is like you say, "hey, i can edit your unit to make it read & write again"..... Pointless discussion i think when developer has power to do all. ---------------------------------------------------------------------- KodeZwerg · 2022-09-05 20:49 Quote from: MarkMLl on September 05, 2022, 07:36:28 pmBeing able to restrict accessibility is a fundamental issue of language robustnessFor that purpose the makers of Pascal gave us the "const" ---------------------------------------------------------------------- PascalDragon · 2022-09-06 05:55 Quote from: MarkMLl on September 05, 2022, 07:36:28 pmQuote from: PascalDragon on September 05, 2022, 01:34:45 pmWe won't introduce anything else here, because this would be a whole new thing to only solve a problem which in my personal opinion isn't even a problem that needs to be dealt with. I'm afraid that this is something on which we'll have to disagree. Being able to restrict accessibility is a fundamental issue of language robustness, and its implementation is far too important to be left to the vagaries of the compiler (specifically, whether it deigns to honour the inline directive). Then we'll disagree here. You will essentially get your wish once we support ISO Extended Pascal modules in addition to units because that allows a more fine grained control about what is exported. But no one is currently working on that so don't expect anything soon. Also even if you don't use inline or the compiler does not respect it, the performance impact shouldn't be that much. We're talking about a call and returning a value here. Considering that there are no branches modern CPUs can probably handle that with their branch prediction and such. Quote from: KodeZwerg on September 05, 2022, 10:49:27 pmQuote from: MarkMLl on September 05, 2022, 07:36:28 pmBeing able to restrict accessibility is a fundamental issue of language robustnessFor that purpose the makers of Pascal gave us the "const"  const is not a suitable solution for the problem at hand. ---------------------------------------------------------------------- MarkMLl · 2022-09-06 07:54 Quote from: PascalDragon on September 06, 2022, 07:55:57 amQuote from: KodeZwerg on September 05, 2022, 10:49:27 pmFor that purpose the makers of Pascal gave us the "const"  const is not a suitable solution for the problem at hand. There is one situation where superficially it might appear to be: 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 // WARNING: COMPLETELY UNTESTED AND PROBABLY INVALID CODE FOLLOWS.unit unit_a; type  TWriteConsole= procedure(const str: string; eol: boolean= false); procedure WriteConsole(const str: string; eol: boolean= false); procedure DoStuff(wc: TWriteConsole; const str: string);...end. program test; uses  unit_a; var  someString: string; begin  DoStuff(@WriteConsole, someString)end.  At first glance, an inexperienced programmer would assume that that is a valid solution since the original writer of "test" and "unit_a" has passed the string as a constant parameter. However in practice it is flawed since a maintainer might remove the "const" from the declarations in unit_a, and the main program has absolutely no way of knowing that or objecting. Relying on "goodwill" or "commonsense" without adequate safeguards is, in my opinion, extremely poor practice. MarkMLl ---------------------------------------------------------------------- serbod · 2022-09-06 10:57 Quote from: MarkMLl on September 05, 2022, 12:53:22 pmOP has specifically asked about a variable and has specifically said that he is concerned about the overhead of using a function either directly or as the accessor of a property. Then just change constant value in runtime. It safe only for non- manageable types. 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 const  ConstInt: Integer = 123; var  VarInt: Integer absolute ConstInt; begin  WriteLn('ConstInt=', ConstInt);  VarInt := 321;  WriteLn('ConstInt=', ConstInt);  ReadLn();end. ---------------------------------------------------------------------- PascalDragon · 2022-09-06 11:35 Quote from: serbod on September 06, 2022, 12:57:43 pmThen just change constant value in runtime. It safe only for non-manageable types. 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 const  ConstInt: Integer = 123; var  VarInt: Integer absolute ConstInt; begin  WriteLn('ConstInt=', ConstInt);  VarInt := 321;  WriteLn('ConstInt=', ConstInt);  ReadLn();end. This example will fail with disabled writeable consts (cause the value will be in the rdata section of the binary) and if writable consts are enabled you don't need to use the variable, cause you can set the constant directly. ---------------------------------------------------------------------- MarkMLl · 2022-09-06 12:34 Quote from: PascalDragon on September 06, 2022, 01:35:14 pmThis example will fail with disabled writeable consts (cause the value will be in the rdata section of the binary) and if writable consts are enabled you don't need to use the variable, cause you can set the constant directly. I'll warrant that it's an interesting try though since it does concentrate all of the elements at the point of declaration. Can the variable (aliased using absolute) be moved into the implementation part, leaving the R/O constant in the interface part preceded by {$WRITEABLECONST OFF}? I suppose a more pressing question is whether an aliased variable (or for that matter a pointer to the constant which allows it to be written) can also be created in a different unit. Even so I think it answers the OP... MarkMLl ---------------------------------------------------------------------- PascalDragon · 2022-09-07 07:41 Quote from: MarkMLl on September 06, 2022, 02:34:10 pmQuote from: PascalDragon on September 06, 2022, 01:35:14 pmThis example will fail with disabled writeable consts (cause the value will be in the rdata section of the binary) and if writable consts are enabled you don't need to use the variable, cause you can set the constant directly. I'll warrant that it's an interesting try though since it does concentrate all of the elements at the point of declaration. Can the variable (aliased using absolute) be moved into the implementation part, leaving the R/O constant in the interface part preceded by {$WRITEABLECONST OFF}? While valid in the sense of the language it wouldn't solve the problem: if you declare the constant as {$WRITEABLECONST OFF} you can not change it no matter how many variable aliases you provide, because the storage location will be the binary's rdata (aka read only data!) section and thus you'll get an access violation if you try to write to the variable. Quote from: MarkMLl on September 06, 2022, 02:34:10 pmI suppose a more pressing question is whether an aliased variable (or for that matter a pointer to the constant which allows it to be written) can also be created in a different unit. Even so I think it answers the OP... As soon as you can alias a variable or constant it solely depends on the underlying storage location (rdata or data) whether it can be modified at all. Either everyone in the executable can modify it or no one can. ---------------------------------------------------------------------- MarkMLl · 2022-09-07 07:59 Quote from: PascalDragon on September 07, 2022, 09:41:58 amWhile valid in the sense of the language it wouldn't solve the problem: if you declare the constant as {$WRITEABLECONST OFF} you can not change it no matter how many variable aliases you provide, because the storage location will be the binary's rdata (aka read only data!) section and thus you'll get an access violation if you try to write to the variable. As soon as you can alias a variable or constant it solely depends on the underlying storage location (rdata or data) whether it can be modified at all. Either everyone in the executable can modify it or no one can. So basically unworkable, leaving the only possibility being a property with an inlined getter. MarkMLl ---------------------------------------------------------------------- PascalDragon · 2022-09-07 08:02 Quote from: MarkMLl on September 07, 2022, 09:59:20 amSo basically unworkable, leaving the only possibility being a property with an inlined getter. Correct. ---------------------------------------------------------------------- Curt Carpenter · 2022-09-07 15:51 Thanks for the ideas and the interesting discussion.  I can see that what I was looking for is more involved than I had thought. I wasn't expecting anything to be "introduced" (PascalDragon's comment above), but rather that there was already a solution in fpc that I had just never encountered -- this being the case 99.9 percent of the time in my own personal experience.  I do agree with MarkMLI though that anything a compiler can do (at compile time) to encourage discipline in variable accessibility is useful.  Just look at the protections provided for objects.  I started using pascal on a s-100 bus computer with only 64K of memory and two 8" floppy disks, and my view of "overhead" necessarily had the dimensions of both execution time and memory use.  I know that this mindset is archaic anymore, but I still think twice about declaring objects for their memory overhead as well as their possible execution time needs (and subconsciously count pushes and pops in function calls).  Old habits die hard.  (Agree that using global variables is another old habit that is totally out of step with the OOP paradigm though and maybe should die.  Still, I find their use clarifying of my intent and purpose in my own programs.) ---------------------------------------------------------------------- MarkMLl · 2022-09-07 16:23 You had floppies? Luxury! I remember when 8" floppies were such rarefied and expensive technology that I saw a technician making up a replacement spider to hold the medium in place. MarkMLl ---------------------------------------------------------------------- jamie · 2022-09-07 16:49 I have some 12" floppies here. I keep them around for whatever. I think I still have the drive for them, too. ---------------------------------------------------------------------- Curt Carpenter · 2022-09-07 17:00 The amount of progress has been truly astonishing, and the technology has lapped me four or five times since the race started   I look at the specs for the RasberryPi pico and it blows my mind. I don't think I ever saw a 12".  You might try offering it for sale on Ebay.  I finally sold my 8" a few years ago and was amazed at the price I ended up getting for them.  There's apparently an active group of collectors for the "ancient" stuff out there. ---------------------------------------------------------------------- MarkMLl · 2022-09-07 17:44 Quote from: Curt Carpenter on September 07, 2022, 07:00:01 pmI don't think I ever saw a 12".  You might try offering it for sale on Ebay.  I finally sold my 8" a few years ago and was amazed at the price I ended up getting for them.  There's apparently an active group of collectors for the "ancient" stuff out there. He measures diagonally... MarkMLl ---------------------------------------------------------------------- BobDog · 2022-09-07 22:22 I remember turbo pascal on two large floppy's in an Olivetti machine. It came with an editor(ide), a little bit more sophisticated than Quick Pascal. That was quite a while ago. If I had stuck to pascal then I might be better at it now, but I drifted off, and it is only a might Anyway, functions (by reference), one is a variable, one a constant. Only de-referenced access to these functions need be used. If you try to change the constant, no crash or warning, it just doesn't change. You have an option of printing a warning yourself if required. Just for fun, and no real use for these (by reference) functions. 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 function X: pansistring;constz:ansistring='changeable';beginexit(@z);end; function Y:pansistring;constz:ansistring='';k='unchangeable';beginz:=k;exit(@z) ;end; function pi:pdouble;constz:double=0.0;k=3.141592653589793;beginif (z<>0.0) and (single(z)<>single(k)) then writeln('The value of pi is a constant here!');z:=k;exit(@z);end;    beginwriteln(x^);x^:='goodbye ';writeln(x^+x^+x^); writeln(y^);y^:='123';writeln(y^); y^:=y^+ x^;writeln(y^); writeln(pi^);pi^:=100;writeln(pi^);  x^:='Press return to end . . .';writeln(x^);readln;end. ---------------------------------------------------------------------- jamie · 2022-09-07 23:32 Do I need to go to my archives and place a ruler on it to show you? No, In fact I won't waste my time because I know where they are, and it requires moving some heavier stuff to get to them. But the drive is a big door on top you lift to put the disk in and it's only single sided on the hardware end, you need to flip it by hand for the other side. ---------------------------------------------------------------------- MarkMLl · 2022-09-08 07:48 Quote from: jamie on September 08, 2022, 01:32:04 amDo I need to go to my archives and place a ruler on it to show you? Stop being silly. Actually, are these in a fairly rigid case about 1/2" thick, rather than the usual flimsy envelope? It strikes me that you might be talking about a Bernoulli drive, which is probably worth a bit these days (i.e. worth even more than an 8" drive in good condition, which itself will be sought-after). MarkMLl ---------------------------------------------------------------------- jamie · 2022-09-08 11:13 This was part of an early attempt to convert over CNC machines. It's machine shop equipment that loaded the cuts on the floppy. The whole job could be put on these floppies. The old IBM had hard sector of 12 / 14 inch and up of course at one time.  Some early models of desktops used 8-inch standard inserts and some outbound drives using 10.  One of the old Tandy computers used the 8. The attempt was also tried using the 8-inch drives (Tandy) for the CNC mill shop but back then, there were way too many hardware issues, failures.  Because memory was short for this process the CNC actually did constant reads from the floppy most of the time and creating the job was tedious.  Of the Disks I have here in my storage, have actual jobs on them. I am sure they are still readable but are also printed out in the old punch code.  I also hang out in the electronics world, that was first before programming which kind of mixed into it at some point at some point.  I have some antique flip flop tube assemblies here. ---------------------------------------------------------------------- Curt Carpenter · 2022-09-08 15:33 Fun to reminisce. One of my first projects as a new engineer involved creating a factory network that allowed machinists to request tools and CNC programs from a central tool crib.  The big design decision was whether or not to use the then brand-new Intel 8008 (we decided against it!).  Each machine station had a network connection, a CRT and light pen for input.  We were all so proud of it