Recent

Author Topic: record default  (Read 1385 times)

ginoo

  • Full Member
  • ***
  • Posts: 139
record default
« on: September 01, 2025, 07:03:07 am »
Code: Pascal  [Select][+][-]
  1.  ciccio = record
  2.     a : Integer;
  3.     class operator Initialize(var value:ciccio);
  4.   end;
  5.  
  6. var
  7.   c : ciccio;
  8.  
  9. { ciccio }
  10.  
  11. class operator ciccio.Initialize(var value: ciccio);
  12. begin
  13.   value.a:=3;
  14. end;
  15.  
  16. begin
  17.   WriteLn(c.a);
  18.   c.a := 10;
  19.   WriteLn(c.a);
  20.   c := Default(ciccio);
  21.   WriteLn(c.a);
  22.  
  23. end.    
  24.  


In my opinion, calling the record's default should call Initialize and set ciccio.a = 3.
What is the way, if any, to reinitialize a record after modifying it?
« Last Edit: September 03, 2025, 11:54:29 am by ginoo »

Thaddy

  • Hero Member
  • *****
  • Posts: 18364
  • Here stood a man who saw the Elbe and jumped it.
Re: record default
« Reply #1 on: September 01, 2025, 07:07:10 am »
The behavior of Default() is as documented. To reset the record to 3, set the field to 3
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Khrys

  • Sr. Member
  • ****
  • Posts: 348
Re: record default
« Reply #2 on: September 01, 2025, 08:24:43 am »
What is the way, if any, to reinitialize a record after modifying it?

Code: Pascal  [Select][+][-]
  1. Initialize(c);

ginoo

  • Full Member
  • ***
  • Posts: 139
Re: record default
« Reply #3 on: September 01, 2025, 09:30:27 am »
What is the way, if any, to reinitialize a record after modifying it?

Code: Pascal  [Select][+][-]
  1. Initialize(c);

It works.
Does it make sense to also call "finalize" at the end after calling "Initialize," or is it not important since it's a non-dynamic variable?

Thaddy

  • Hero Member
  • *****
  • Posts: 18364
  • Here stood a man who saw the Elbe and jumped it.
Re: record default
« Reply #4 on: September 01, 2025, 10:03:01 am »
Only if your record contains managed types.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

ginoo

  • Full Member
  • ***
  • Posts: 139
Re: record default
« Reply #5 on: September 01, 2025, 10:11:51 am »
Only if your record contains managed types.

Dear @Thaddy, I'm considering using inizialeze because it's cleaner to have a single initializer for record fetching with a lot of code. There are probably other ways, but I think this one is valid.

Thaddy

  • Hero Member
  • *****
  • Posts: 18364
  • Here stood a man who saw the Elbe and jumped it.
Re: record default
« Reply #6 on: September 01, 2025, 10:21:50 am »
I was referring to finalize...
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Warfley

  • Hero Member
  • *****
  • Posts: 2021
Re: record default
« Reply #7 on: September 01, 2025, 01:21:38 pm »
In my opinion, calling the record's default should call Initialize and set ciccio.a = 3.
What is the way, if any, to reinitialize a record after modifying it?

Yes your opinion is right and in trunk this is fixed.

Until it is in stable instead of using default do the following:
Code: Pascal  [Select][+][-]
  1. generic procedure SetDefault<T>(var AVar: T);
  2. begin
  3.   Finalize(AVar);
  4.   Initialize(AVar);
  5. end;
Or
Code: Pascal  [Select][+][-]
  1. generic function ManagedDefault<T>:T;
  2. begin {nothing needed} end;

ginoo

  • Full Member
  • ***
  • Posts: 139
Re: record default
« Reply #8 on: September 01, 2025, 02:04:46 pm »
Just curious, I tried putting a managed type on the record and not using finalize. However, it says there's no memory leak. What's the problem with not calling "finalize"?
Code: Pascal  [Select][+][-]
  1. type
  2. ciccio = record
  3.    a : String;
  4.    class operator Initialize(var value:ciccio);
  5.  end;
  6.  
  7. var
  8.  c : ciccio;
  9.  
  10. { ciccio }
  11.  
  12. class operator ciccio.Initialize(var value: ciccio);
  13. begin
  14.  value.a:='pippo';
  15. end;
  16.  
  17. begin
  18.  WriteLn(c.a);
  19.  c.a := 'caio';
  20.  WriteLn(c.a);
  21.  Initialize(c);
  22.  c.a := 'caio2';
  23.  WriteLn(c.a);
  24.  Initialize(c);
  25.  c.a := 'caio3';
  26.  WriteLn(c.a);
  27.  Initialize(c);
  28.  WriteLn(c.a);
  29. end.
  30.  
     
0 memory blocks allocated : 0/0
0 memory blocks freed     : 0/0
0 unfreed memory blocks : 0
True heap size : 0
True free heap : 0

« Last Edit: September 03, 2025, 11:55:20 am by ginoo »

Warfley

  • Hero Member
  • *****
  • Posts: 2021
Re: record default
« Reply #9 on: September 01, 2025, 02:58:14 pm »
Because StringConstants are not reference counted, because they are hardcoded in the program data.

Change your code to the following:
Code: Pascal  [Select][+][-]
  1. c.a:='calo';
  2. UniqueString(c.a);
  3. ...
And try again. UniqueString makes sure that you have a unique copy of the string, which allocates a new memory object

ginoo

  • Full Member
  • ***
  • Posts: 139
Re: record default
« Reply #10 on: September 01, 2025, 03:12:53 pm »
Yes, you're right. Thank you all.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6195
  • Compiler Developer
Re: record default
« Reply #11 on: September 02, 2025, 09:15:12 pm »
  ciccio = record
    a : Integer;
    class operator Initialize(var value:ciccio);
  end;

var
  c : ciccio;
[snip]

Please use [code][/code]-tags to make the code better visible and also to avoid the forum software potentially interpreting the code.

ginoo

  • Full Member
  • ***
  • Posts: 139
Re: record default
« Reply #12 on: September 03, 2025, 11:56:35 am »
Done

PascalDragon

  • Hero Member
  • *****
  • Posts: 6195
  • Compiler Developer
Re: record default
« Reply #13 on: September 05, 2025, 10:42:40 pm »

 

TinyPortal © 2005-2018