const variable can be modified in FP? lazarus / 62-General ---------------------------------------------------------------------- ssawgift · 2022-05-21 15:05 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 test; const  SomeValue: integer = 1234; begin  SomeValue := 0;end. The above code actually compiles and runs. I tested this code in Delphi 7 which produces expected error stating you cannot modify a const variable. Am I missing something? ---------------------------------------------------------------------- jamie · 2022-05-21 15:11 Yes, that is normal It's basically a writeable constant. When ever you apply a type to it then becomes a writeable constant which basically means it gets loaded into memory at program load time.  you can turn this off but you'll find that feature very desirable.  In other languages they call it differently, STATIC for example comes to mind.. ---------------------------------------------------------------------- ssawgift · 2022-05-21 15:39 Quote from: jamie on May 21, 2022, 05:11:10 pmYes, that is normal It's basically a writeable constant. When ever you apply a type to it then becomes a writeable constant which basically means it gets loaded into memory at program load time.  you can turn this off but you'll find that feature very desirable.  In other languages they call it differently, STATIC for example comes to mind.. Then I have a natural followon question - what is the difference between a var and a const in FP? ---------------------------------------------------------------------- marcov · 2022-05-21 15:44 Quote from: ssawgift on May 21, 2022, 05:39:29 pmThen I have a natural followon question - what is the difference between a var and a const in FP? First you have two types. Notation without : are pure constants.   (like const y= 5  ;  or const y=longint(5). Then you have so called typed consts (const y: integer =5;), which are initialized variables depending on the $J+ directive or  {$writableConst}. Delphi 4+ changed strategy and also allowed you to have initialized global variables   (var y : integer =5;), to make intentions clearer. ---------------------------------------------------------------------- howardpc · 2022-05-21 16:16 The default setting for {$J} in commonly used fpc modes is {$J+} which is why the code in your initial post gave the typed const the behaviour of an initialised variable. If you set {$J-} in that code:program test;{$mode objfpc}{$J-} const  SomeValue: integer = 1234;begin  SomeValue := 0;end.then you cannot compile and get a message such as test.lpr(10,3) Error: Can't assign values to const variable ---------------------------------------------------------------------- Handoko · 2022-05-21 16:24 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 procedure TForm1.Button1Click(Sender: TObject);var  a: Integer;const  b = 1;  c : Integer = 1;begin  a := a + 1;  b := b + 1;  c := c + 1;  ShowMessage(a.ToString + ' ' + b. ToString + ' ' + c.ToString);end; a (line #3) is a variable. When entering the procedure the value of a is undefined. Usually we need to initialize the variable before using it. Except when using it for passing as a out-parameter for a procedure/function. When re-entering the procedure/function the value will become undefined, it can be anything. b (line #5) is a 'true' constant. The value of b will be same forever and cannot be changed. So line #9 will cause a compile-time error. c (line #6) is a typed constant. If writable constant is enabled, the value of c will be kept when exiting the procedure. The name typed constant is a bit misleading. But here it means the value will be unchanged when exiting the procedure/function. Remove the line #9 and run the button-click even several times, if you want to better understand the different. ---------------------------------------------------------------------- ssawgift · 2022-05-21 16:36 Quote from: jamie on May 21, 2022, 05:11:10 pm you can turn this off but you'll find that feature very desirable. Can you list some scenarios where this feature is useful? ---------------------------------------------------------------------- Handoko · 2022-05-21 16:40 Type constant is useful, I often use it as a switch - to remember something has been done. Same thing can be achieved without typed constant but you have to use a global variable or variable at the outer block. This link below has a collection of demos: https://wiki.freepascal.org/Portal:HowTo_Demos Click the link above and find these names and lines: Binary file demo - line 67Game Map Editor - line 183, 184Fading images using BGRABitmap - line 84, 85, 86Button with animation - line 39 ---------------------------------------------------------------------- Mr.Madguy · 2022-05-21 19:13 I'm not 100% sure, but I guess, consts should be created in "const" executable image section, i.e. read-only section. I.e. such code should compile, but it should cause access violation at runtime. I guess, compiler optimizes such code some way, so such variable is created on stack, that is obviously writable. ---------------------------------------------------------------------- 440bx · 2022-05-21 19:41 Quote from: ssawgift on May 21, 2022, 06:36:30 pmCan you list some scenarios where this feature is useful? these "writeable constants" (horrible name) are useful when a function/procedure wants to keep track of some value across calls.  For instance: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 MyFunction(Parameter : TSOMETYPE) : boolean;const   PersistentCallCount : DWORD = 0;var   SomeVar       : DWORD;begin   result := FALSE;   { keep track of the number of times this function has been called }   inc(PersistentCallCount);     SomeVar := ;   { presumably use SomeVar in subsequent statements  }   { statement ... }  { statement ... }  { statement ... }   result := TRUE;end; The variable PersistentCallCount, unlike SomeVar, _retains_ its value when the function is exited.  The first time the function is called, its value is zero, the second time, it's value is 1, the third time its value is 2 and so on. OTH, the initial value of SomeVar when the function is called is completely unpredictable and for that reason it must be initialized every time the function is called with a value that is presumably useful. A very important characteristic of typed constants is that their write-ability is controlled by the {$J} directive.  When on {$J+} the compiler will place it in a writeable data segment (same area of the executable it would place a global variable), when off {$J-} it will place it in a read-only section of the executable (if the O/S supports such sections.) Also, important is to be aware that the setting of $J can be changed as many times as necessary to control whether or not the "constant" is writeable (Thanks to @benibela for pointing this out in another thread.) Basically, a writeable constant is a global variable whose visibility is limited to the function/procedure it is declared in.   It is still global because the function could include a parameter that returns a pointer to it (such as @PersistentCallCount) which would enable outer functions to change its value but, that is not only cumbersome and very poor programming, it totally defeats the reason to declare it in the function/procedure's scope. The equivalent of an FPC writeable constant in C is a "static" variable. HTH. ---------------------------------------------------------------------- ssawgift · 2022-05-22 02:50 Quote from: 440bx on May 21, 2022, 09:41:29 pmthese "writeable constants" (horrible name) are useful when a function/procedure wants to keep track of some value across calls.  This one cleared cloud in my head. I agree the name 'writeable constants' is a horrible name in that it's not logical at all, and it kills my brain. It really deserves a dedicated keyword like static in C/C++ or something similar. ---------------------------------------------------------------------- PascalDragon · 2022-05-23 11:27 Quote from: Mr.Madguy on May 21, 2022, 09:13:52 pmI'm not 100% sure, but I guess, consts should be created in "const" executable image section, i.e. read-only section. I.e. such code should compile, but it should cause access violation at runtime. I guess, compiler optimizes such code some way, so such variable is created on stack, that is obviously writable. No stack involved. Typed constants with writable constants enabled are simply placed in the same image section as initialized variables. And if writeable constants are off then the compiler will already do assignment checks at compile time though some might escape to runtime (e.g. if you take a pointer to a typed constant and try to write to that). Quote from: ssawgift on May 22, 2022, 04:50:53 amIt really deserves a dedicated keyword like static in C/C++ or something similar. It is this way since Turbo Pascal. Nothing will be changed there. ---------------------------------------------------------------------- winni · 2022-05-23 15:48 Hi! The introduction of Typed Constants was made by Borland in the early 80s. There was a lot of discussion about sense and non-sense of the Typed Constants. But after 40 years there will be no change. From: TURBO Pascal Reference Manual Version 3.0 Copyright C> 1983, 1984, 1985 by BORLAND INTERNATIONAL Inc. ================================ Chapter 13 TYPED CONSTANTS Typed constants are a TURBO specialty. A typed constant may be used exactly like a variable of the same type. Typed constants may thus be used as "initialized variables", because the value of a typed constant is defined, whereas the value of a variable is undefined until an aSSignment is made. Care should be taken, of course, not to assign values to typed constants whose values are actually meant to be constant. The use of a typed constant saves code if the constant is used often in a program, because a typed constant is included in the program code only once, whereas an untyped constant is included every time it is used. Typed constants are defined like untyped constants (see page 40), except that the definition specifies not only the value of the constant but also the type. In the definition the typed constant identifier is succeeded by a colon and a type identifier, which is then followed by an equal sign and the actual constant ================================ Found in my bookshelf and here: http://bitsavers.informatik.uni-stuttgart.de/pdf/borland/turbo_pascal/ TURBO_Pascal_Reference_Manual_CPM_Version_3_Dec88.pdf Winni