Recent

Author Topic: Initialisation of constants vs variables in records and classes  (Read 9047 times)

JdeHaan

  • Full Member
  • ***
  • Posts: 171
Re: Initialisation of constants vs variables in records and classes
« Reply #15 on: August 19, 2025, 05:33:56 pm »
I knew it worked for constant declarations, so just tried it out for a var declaration.

Also works for local variables:

Code: Pascal  [Select][+][-]
  1. procedure InitRec(var Rec: TRec);
  2. var
  3.   aRec: TRec = (Field: True);
  4. begin
  5.   Rec := aRec;
  6. end;
  7.  
« Last Edit: August 19, 2025, 05:38:27 pm by JdeHaan »

PascalDragon

  • Hero Member
  • *****
  • Posts: 6396
  • Compiler Developer
Re: Initialisation of constants vs variables in records and classes
« Reply #16 on: August 19, 2025, 09:15:07 pm »
But I am not sure I'll use it in anything "production" unless I hear eg PascalDragon bless it !

It's documented in the Language Reference Guide in 4.4 Initialized Variables (link goes to the Internet Archive, cause our server is currently offline after an update) and exists since Turbo Pascal (though there only for constants).

dbannon

  • Hero Member
  • *****
  • Posts: 3825
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Initialisation of constants vs variables in records and classes
« Reply #17 on: August 20, 2025, 04:00:40 am »
It's documented in the Language Reference Guide in 4.4 Initialized Variables (link goes to the Internet Archive, cause our server is currently offline after an update) and exists since Turbo Pascal (though there only for constants).

Wow, it is here too. I must have read every 'relevant' bit of that ref.pdf three times looking for it and still missed it. In fact, if you look, I referred  JdeHaan to the subsection immediately after, 4.5 Initializing variables using default and still missed it.

There are parts of ref that are not particularly clear. In this case, we could have mention of 4.4 in the text for both 3.3.2 Records Types and 9 Extended Records (ie Advanced Records). I repeatedly read both those sections looking for initialization.

Are those documents ever reviewed ? My first read of ref.pdf in particular almost caused me to give on using FPC for my project. I sure would like to contribute in that space.

Thanks PascalDragon, again !

Davo

Lazarus 4, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

n7800

  • Hero Member
  • *****
  • Posts: 709
  • Lazarus IDE contributor
    • GitLab profile
Re: Initialisation of constants vs variables in records and classes
« Reply #18 on: August 21, 2025, 05:11:00 am »
Are those documents ever reviewed ? My first read of ref.pdf in particular almost caused me to give on using FPC for my project. I sure would like to contribute in that space.

There is a bug tracker for this documentation: https://gitlab.com/freepascal.org/fpc/documentation/-/issues

Note that this page contains the documentation for the latest released version: https://www.freepascal.org/docs.html

The updated (not yet released) documentation can be found here: https://www.freepascal.org/daily/daily.html

It is recommended to review it before creating an issue, as it may already have this fixed.

dbannon

  • Hero Member
  • *****
  • Posts: 3825
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Initialisation of constants vs variables in records and classes
« Reply #19 on: August 25, 2025, 08:54:01 am »
Thanks n7800, below is a copy of some of the notes I took last time I loked at the Language Reference. Maybe I am just being a nit picker (and I can be) but I consider the new user here. What do you think ?

----------------

5.4 Class or Static fields
In the demo code -
The first and fifth writeln() expect a string literal, however, are missing the inverted comma treatment. Obvious typo to someone with some experience, baffling to a newbie

(Pedantic) declaring a type 'cl' is inconsistent with best practice. TCl  perhaps, better to use 'TObj' ?
Using a lowercase L mixed in with the numeral 1 makes it very hard to read with some fonts. TObj is far more readable.


5.5 Constructors and Destructors
Say "A constructor/destructor pair is required if the object uses virtual methods." However, no mention of how the programmer initiates that. Does it just happen because a constructor exits, is called or because it contains "inherited <something>" ?

5.6.1   Declaration
Has a small block of code demonstrating that fields can be declared after methods in a block, but, in fact, the field is declared before the method so, illustrates nothing.

5.6.2 Method invocation
Under "Abstract methods" we are told that Line 3 will generate a compile error. No idea where or what Line 3 is. Recreate the code the author is referring to, its "inherited Doit;" in the implementation of TChildDoIt, reading the text would never arrive there !. That line of code does not appear anywhere in "Abstract methods" heading. Where the 3 came from is anyone's guess.

and, just added

3.3.2 Record Types
9 Extended Records
Both sections could do with a mention that 4.4. Initialized Variables has an example of initializing a record.

And, while here, why "Extended Records" in the documentation. The switch to turn it on is {$modeswitch advancedrecords}. When spoken of in the forum, Extended Records gets 27 mentions. Advanced Records gets 215.

----------------

I had a ComSci lecturer once who said documentation should be like a good adult movie. Clear, well focused and leave absolutely nothing to the imagination.
 
Davo
Lazarus 4, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

Thaddy

  • Hero Member
  • *****
  • Posts: 19265
  • Glad to be alive.
Re: Initialisation of constants vs variables in records and classes
« Reply #20 on: August 25, 2025, 01:23:26 pm »
For the adult movie don't forget to explain what happens here, because few understand it.
Code: Pascal  [Select][+][-]
  1. const
  2. {$push}{$J- as true constant }
  3.   PCG32_INITIALIZER:pcg32_random_t = (state:qword($853c49e6748fea9b);
  4.                                       inc:qword($da3e39cb94b95bdb));
  5. {$J+ as static variable, this is initial state }
  6.    pcg32_global:pcg32_random_t = (state:qword($853c49e6748fea9b);
  7.                                   inc:qword($da3e39cb94b95bdb));
  8. {$pop}
Assignable typed const is Pascal speak for a static variable, alas.
This is live code...and correct.
« Last Edit: August 25, 2025, 01:52:21 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

Warfley

  • Hero Member
  • *****
  • Posts: 2066
Re: Initialisation of constants vs variables in records and classes
« Reply #21 on: August 25, 2025, 02:24:30 pm »
But unfortunately this only works for constants. I now use writable constants as variables.
Is there a reason why variables in a record or class definition cannot be initialised?

Very simply: Because non managed types are not initialized.

For example:
Code: Pascal  [Select][+][-]
  1. procedure Foo;
  2. var
  3.   i: Integer;
  4. begin
  5.   WriteLn(i);
  6. end;
  7.  
  8. procedure Bar;
  9. var
  10.   i: Integer;
  11. begin
  12.   i:=42;
  13. end;
  14.  
  15. begin
  16.   Foo; // Prints 5440
  17.   Bar;
  18.   Foo; // Prints 42
  19. end.
  20.  
Because the variable in Foo is not initialized it's filled with the random memory thats in place when the function get's called. There are manged types, which are initialized, most notably the AnsiString type:
Code: Pascal  [Select][+][-]
  1. procedure Foo;
  2. var
  3.   s: AnsiString;
  4. begin
  5.   WriteLn(s);
  6. end;
  7.  
  8. procedure Bar;
  9. var
  10.   s: AnsiString;
  11. begin
  12.   s:='Hello World';
  13. end;
  14.  
  15. begin
  16.   Foo; // Prints empty
  17.   Bar;
  18.   Foo; // Still Empty
  19. end.
  20.  

You can turn your record into a managed record by overloading the management operators: https://wiki.freepascal.org/management_operators

There is in ExtPas the ability to specify the initial state:
Code: Pascal  [Select][+][-]
  1. type
  2.   TMyRec = record
  3.     a: Integer;
  4.   end value [a: 42];
This is not (yet) supported by FPC

dbannon

  • Hero Member
  • *****
  • Posts: 3825
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Initialisation of constants vs variables in records and classes
« Reply #22 on: August 26, 2025, 02:56:41 am »
Very simply: Because non managed types are not initialized.

Worth noting that often non managed types 'appear' to be initialized to 0, nil etc. Don't believe it, sooner or later it will bite you !

Davo
Lazarus 4, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

Thaddy

  • Hero Member
  • *****
  • Posts: 19265
  • Glad to be alive.
Re: Initialisation of constants vs variables in records and classes
« Reply #23 on: August 26, 2025, 09:02:19 am »
Worth noting that often non managed types 'appear' to be initialized to 0, nil etc. Don't believe it, sooner or later it will bite you !
As per the manual, global (heap) variables are always initialized. The problems start with local (stack) variables. You should always initialize locals.
It doesn't hurt to initialize globals, but strictly speaking that is not necessary.
objects are fine constructs. You can even initialize them with constructors.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6396
  • Compiler Developer
Re: Initialisation of constants vs variables in records and classes
« Reply #24 on: September 02, 2025, 09:40:33 pm »
Worth noting that often non managed types 'appear' to be initialized to 0, nil etc. Don't believe it, sooner or later it will bite you !
As per the manual, global (heap) variables are always initialized. The problems start with local (stack) variables. You should always initialize locals.

Global <> Heap. Heap is for dynamically allocated memory during the runtime of the application while global variables are allocated by the operating system at program start due to them being part of the binary (.data or .bss section).

 

TinyPortal © 2005-2018