Forum > General

Writable consts...?

<< < (2/5) > >>

Thaddy:

--- Quote from: cdbc on July 11, 2024, 10:31:25 am ---Hi
Thaddy is that just in the implementing unit or also in the units using it?!?
Regards Benny

--- End quote ---
No, it is the way compiler handles it: in {$J-} state it ends up in a read-only section.
That means it should be global. You can't turn that around without hacks.

440bx:

--- Quote from: cdbc on July 11, 2024, 10:31:25 am ---... is that just in the implementing unit or also in the units using it?!?
Regards Benny

--- End quote ---
you didn't ask me but, the writeability of the const is determined by the setting of the J directive where the const is declared. 

Since the J directive can change multiple times and at any time in a unit or program it is therefore not a unit "thing".  Its state can be "on" at some point, "off", twenty lines later and back "on" ten lines after that.  Turn it on and off to your liking and/or needs.

HTH.

Thaddy:
No, once it is declared it keeps the state.  Always.
Simply compile with -s and examine...

cdbc:
Hi
Ok, so if I define it like this:
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit model.base;{$mode ObjFPC}{$H+}{$J+} /// <--- here?!?{$ModeSwitch advancedrecords}interfaceuses classes,sysutils,obs_prosu;const  { (p)rovider (r)easons for observer action/reaction }  prStatus = prUser + 1; { carries an optional object in aNotifyClass and usually a pchar in UserData }  prDataStrNeeded = prUser + 2; { carries an 'ItrxSectionFetch' obj in UserData }  prSectionFetched = prUser + 3;     { carries an 'IStrings' object in UserData }  prStaticTextsFetched = prUser + 4; { carries an 'IStrings' object in UserData }  prUnitFetched = prUser + 5;        { carries an 'IStrings' object in UserData }type{$interfaces corba}  { this is the ancester }  IbcCorba = interface['{3563A63E-8C1A-40E1-9574-3E4171BB3ACF}']    function Obj: TObject;  end;  TcsuTransaction = class; // forward  { ImemTransaction is our container vessel for changes }  IcsuTransaction = interface['{AFE2A986-7D3C-46AB-A4BF-2A0BDA1DECDF}']    function get_DataPtr: pointer;    function get_Id: ptrint;    function get_ModReason: word;    function get_Sender: TObject;    function get_StrProp(anIndex: integer): shortstring;    function Obj: TcsuTransaction;    procedure set_DataPtr(aValue: pointer);    procedure set_Id(aValue: ptrint);    procedure set_ModReason(aValue: word);      procedure set_Sender(aValue: TObject);    procedure set_StrProp(anIndex: integer;aValue: shortstring);//    function AssignFrom(anItem: ImemItem): boolean;    property DataPtr: pointer read get_DataPtr write set_DataPtr;     property ID: ptrint read get_Id write set_Id;    Property ModReason: word read get_ModReason write set_ModReason;    Property Sender: TObject read get_Sender write set_Sender;     property Title: shortstring index 0 read get_StrProp write set_StrProp;  end;{$interfaces com}  { TcsuTransaction is our container vessel for changes }  TcsuTransaction = class(TObject,IcsuTransaction)  protected    fDataPtr: pointer;    fID: ptrint;    fModReason: word;     fSender: TObject;    fTitle: shortstring;    function get_DataPtr: pointer; virtual;    function get_Id: ptrint; virtual;    function get_ModReason: word; virtual;    function get_Sender: TObject; virtual;    function get_StrProp(anIndex: integer): shortstring; virtual;    function Obj: TcsuTransaction; virtual;    procedure set_DataPtr(aValue: pointer); virtual;    procedure set_Id(aValue: ptrint); virtual;    procedure set_ModReason(aValue: word); virtual;    procedure set_Sender(aValue: TObject); virtual;    procedure set_StrProp(anIndex: integer;aValue: shortstring); virtual;  public    constructor Create(aModReason: word); virtual;    destructor Destroy; override;//    function AssignFrom(anItem: ImemItem): boolean;    property DataPtr: pointer read get_DataPtr write set_DataPtr;    property ID: ptrint read get_Id write set_Id;    Property ModReason: word read get_ModReason write set_ModReason;    Property Sender: TObject read get_Sender write set_Sender;    property Title: shortstring index 0 read get_StrProp write set_StrProp;  end; { TcsuTransaction }   { P/TLogDirsCreated }  PLogDirsCreated = ^TLogDirsCreated;  TLogDirsCreated = record   const ldcDirs: TStringArray = ('common','models','presenters','views');   var    ldcAbreviation: string;    ldcLogText: string;    ldcProjectname: string;    ldcResult: boolean;    ldcRoot: string;  end; implementation It will then keep its state?
P.s.: sorry 440bx  :-[
Regards Benny

Thaddy:
yes, because the compiler keeps the state for you.
If the constant is declared in {$J-} state...

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version