Recent

Author Topic: Questions about initialization  (Read 3410 times)

Okoba

  • Hero Member
  • *****
  • Posts: 528
Re: Questions about initialization
« Reply #15 on: January 08, 2023, 04:29:15 pm »
@PascalDragon, I would love to have your answers on the questions.

Thaddy

  • Hero Member
  • *****
  • Posts: 14210
  • Probably until I exterminate Putin.
Re: Questions about initialization
« Reply #16 on: January 08, 2023, 07:57:12 pm »
Me too....
Specialize a type, not a var.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Questions about initialization
« Reply #17 on: January 09, 2023, 09:30:14 pm »
@PascalDragon, I would love to have your answers on the questions.

If there would have been anything significant to add to what speter wrote, I would have.

Thaddy

  • Hero Member
  • *****
  • Posts: 14210
  • Probably until I exterminate Putin.
Re: Questions about initialization
« Reply #18 on: January 09, 2023, 10:03:04 pm »
@PascalDragon

Well, for one, it is not the documentation for Default() that was incomplete, but the documentation for initialize(). Michael has picked up my report and eleborated a bit more in depth, so fixed it.
The reference comes from Initialize(), not Default().

Basically Default() can be used anywhere as long as there is a distinct type, not - only - an initialized variable.
And that was what the Initialize() entry stated, you may want to read Michael's corrections.
e.g. Default() will not take an array[0..99] of integer ad verbatum, but it will take it after you declare a type TMyFixedIntegerArrayZeroTo99 = array[0..99] of integer. That was the point. I consider it solved.
Btw my report also contains proof.
« Last Edit: January 09, 2023, 10:22:18 pm by Thaddy »
Specialize a type, not a var.

Okoba

  • Hero Member
  • *****
  • Posts: 528
Re: Questions about initialization
« Reply #19 on: January 10, 2023, 09:57:31 am »
@PascalDragon what about Q5 and Q6?

Okoba

  • Hero Member
  • *****
  • Posts: 528
Re: Questions about initialization
« Reply #20 on: February 14, 2023, 10:06:15 am »
I'm still confided about this subject. And adding to the conversation:
Question7:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3.   {$mode Delphi}
  4.  
  5. type
  6.   TTest = record
  7.     I: Integer;
  8.     class operator Initialize(var AValue: TTest);
  9.     class operator Finalize(var AValue: TTest);
  10.   end;
  11.  
  12.   class operator TTest.Initialize(var AValue: TTest);
  13.   begin
  14.     WriteLn('Initialize');
  15.     AValue.I := 0;
  16.   end;
  17.  
  18.   class operator TTest.Finalize(var AValue: TTest);
  19.   begin
  20.     WriteLn('Finalize');
  21.     AValue.I := 0;
  22.   end;
  23.  
  24.   procedure Test1;
  25.   var
  26.     V: TTest;
  27.   begin
  28.     //Initialize
  29.     WriteLn(V.I); //Warning: Local variable "V" of a managed type does not seem to be initialized
  30.     //Finalize
  31.   end;    
  32.  
  33. begin
  34.   Test1;
  35.   ReadLn;
  36. end.                        
  37.  

In this sample, I get warned by the compiler about not initialized variable, but the logs shows as it is initialized at procedure begin and finalized and the end.
So if the type is managed, and I added the Initialize and Finalize operators, and they are called automatically, why there is a warning?
And please keep in mind the Question6. I don't like Initializing the variant two types as it slows down the code and adds an extra copy, and sometimes makes the debugging process harder if you add a log.

Thaddy

  • Hero Member
  • *****
  • Posts: 14210
  • Probably until I exterminate Putin.
Re: Questions about initialization
« Reply #21 on: February 14, 2023, 11:44:57 am »
That warning is indeed pretty irritating and occurs with more constructs that do actually initialize, like setlength() too. There are already related bug reports for it, I believe.
Specialize a type, not a var.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Questions about initialization
« Reply #22 on: February 15, 2023, 11:01:18 pm »
I personally agree that for local variables of managed types (not Result however) the “not initialized” warning/hint is not required. FPK however does not agree... 🤷‍♀️

Okoba

  • Hero Member
  • *****
  • Posts: 528
Re: Questions about initialization
« Reply #23 on: February 25, 2023, 01:27:20 pm »
If the use didn't Initialize the value, eg V: Integer, I agree that the hint is required. But for a managed type that has Initialize operator, and FPC inserts code for Initialize, so it is clearly Initialized, then warning is redundant.
If there is a issue, I can check on that, or I can open one.

VisualLab

  • Sr. Member
  • ****
  • Posts: 291
Re: Questions about initialization
« Reply #24 on: February 25, 2023, 03:50:57 pm »
The docs could have been more specific about Initialize and Finalize applying to managed variable types only.
For instance the pseudo signature could have been given as:
procedure Initialize(
  var T: TAnyManagedType;
  ACount: SizeInt = 1
);

I support this proposal. I also suggest that you include a link to the documentation page that describes managed types in the description of this procedure. You should also complete the documentation page describing the Finalize procedure in the same way.

For comparison, I suggest looking at how it is described in the Delphi documentation: Initialize, Finalize. The description is a bit better (although it could be more detailed).

Unfortunately, there is also a flaw in the descriptions on these pages. If anyone has looked at the given pages of the Delphi documentation, they will have noticed that the documentation editor made mistakes at the beginning of both pages. Both procedures are overloaded. Identical copies are given in the references, which is misleading to people who do not have enough experience. Is:

Code: Pascal  [Select][+][-]
  1. procedure Initialize(var V; [ Count: NativeUInt]); overload;
  2. procedure Initialize(var V; [ Count: NativeUInt]); overload;
  3.  
  4. procedure Finalize(var V; [ Count: NativeUInt]); overload;
  5. procedure Finalize(var V; [ Count: NativeUInt]); overload;

And it should be:

Code: Pascal  [Select][+][-]
  1. procedure Initialize(var V); overload;
  2. procedure Initialize(var V; [ Count: NativeUInt]); overload;
  3.  
  4. procedure Finalize(var V); overload;
  5. procedure Finalize(var V; [ Count: NativeUInt]); overload;

On the other hand, inserting a link to a page that contains a list of procedures and functions that support memory management in the description deserves a plus: Dynamic Memory Allocation Routines.

Such information affects the understanding of the differences between Initialize and Default.

The name of the procedure: Default may also be a problem. It does not reflect well what its purpose is, i.e. fill the variable with zeros (probably even for those who speak English as their native language). Probably a better name for it would be to explicitly call it Zeroing. But now it probably can't be straightened out (using an alias?).

A page like this in the FPC documentation would be useful. Improving the documentation would greatly help people who are just starting to program in Pascal and Object Pascal using FPC (whether or not they have programmed anything before). I myself once had the understanding of how these subroutines work. Among other things, such trifles significantly affect the popularity of the language. And that's what we care about.
« Last Edit: February 25, 2023, 05:27:24 pm by VisualLab »

Thaddy

  • Hero Member
  • *****
  • Posts: 14210
  • Probably until I exterminate Putin.
Re: Questions about initialization
« Reply #25 on: February 25, 2023, 04:25:27 pm »
Specialize a type, not a var.

VisualLab

  • Sr. Member
  • ****
  • Posts: 291
Re: Questions about initialization
« Reply #26 on: February 25, 2023, 04:37:32 pm »
My post was only about suggestions to improve specific pages of the documentation (i.e. Initialize and Finalize). My mention of the Default function name was merely a digression (perhaps redundant).

As for the list of subroutines on the Memory management functions page, I don't see Initialize and Finalize on it. Why?

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Questions about initialization
« Reply #27 on: February 25, 2023, 04:42:11 pm »
If the use didn't Initialize the value, eg V: Integer, I agree that the hint is required. But for a managed type that has Initialize operator, and FPC inserts code for Initialize, so it is clearly Initialized, then warning is redundant.
If there is a issue, I can check on that, or I can open one.

There have been issues about this and FPK closed them as “won't fix”.

A page like this in the FPC documentation would be useful. Improving the documentation would greatly help people who are just starting to program in Pascal and Object Pascal using PFC (whether or not they have programmed anything before). I myself once had the understanding of how these subroutines work. Among other things, such trifles significantly affect the popularity of the language. And that's what we care about.

Then please report an issue for the documentation. It doesn't help if you say that things should be improved, the right people need to know about something being amiss.

Okoba

  • Hero Member
  • *****
  • Posts: 528
Re: Questions about initialization
« Reply #28 on: February 25, 2023, 04:50:31 pm »
@PascalDragon That's unfortunate.
Thank you.

VisualLab

  • Sr. Member
  • ****
  • Posts: 291
Re: Questions about initialization
« Reply #29 on: February 25, 2023, 04:59:20 pm »
A page like this in the FPC documentation would be useful. Improving the documentation would greatly help people who are just starting to program in Pascal and Object Pascal using PFC (whether or not they have programmed anything before). I myself once had the understanding of how these subroutines work. Among other things, such trifles significantly affect the popularity of the language. And that's what we care about.

Then please report an issue for the documentation. It doesn't help if you say that things should be improved, the right people need to know about something being amiss.

OK. But where to report? (URL).

 

TinyPortal © 2005-2018