Tomas Hajny via fpc-pascal <fpc-pascal@lists.freepascal.org> schrieb am Di., 4. Apr. 2023, 09:51:
> On 2023-04-04 09:43, Jacob Kroon wrote: > > > Hi Jacob, > > >> You don't need to change "var" to "const" - if you want to ensure the > >> variables to persist in between the function/procedure runs, you need > >> to move them to the global level, i.e. outside of the > >> functions/procedures. It is not advisable as a general practice (to > >> keep all variables globally), because then you might e.g. access the > >> same variable from multiple functions by mistake, but it would > >> probably solve your issue. Obviously, you might need to solve > >> potential conflicts if you use the same variable names in multiple > >> functions/procedures (e.g. by prepending the function/procedure name > >> to the variable name or something like that). > >> > > > > I could move them to global scope, and add a prefix to avoid name > > collisions, but using a "const" section seems more readable to me > > since I don't need to add a prefix. > > > > The wiki page says (https://wiki.freepascal.org/Const): > > > > "The declaration const in a Pascal program is used to inform the > > compiler that certain identifiers which are being declared are > > constants, that is, they are initialized with a specific value at > > compile time as opposed to a variable which is initialized at run > > time. > > > > However, the default setting in Free Pascal is to allow const > > identifiers to be re-assigned to." > > > > Then it says there is a flag to make reassigning to a const variable > > an error. But if I ignore that, it would seem this is just what I > > need, since this seems to make the variable have a whole-program > > lifetime, no ? > > > > What is the technical downside to using "const", or is it just cosmetic > > ? > > If you read the documentation (wiki or the real documentation in > PDF/HTML etc.) properly, you don't find there anything saying that > constants declared locally within functions or procedures are guaranteed > to keep their modified values across runs of that function/procedure. It > might behave like that, but you shouldn't rely on it unless it's a const > on a global level (and then renaming may still be needed in case of > conflicts). > It *is* documented (the remark at the end): https://www.freepascal.org/docs-html/current/ref/refse10.html#x22-210002.2 Regards, Sven >
_______________________________________________ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal