[question] Management Operator: Copy, AddRef and Finalization lazarus / 62-General ---------------------------------------------------------------------- Warfley · 2020-05-06 16:13 Hey, I have a question about management operators. Take the following example: 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 example; {$mode delphi} var cnt: Integer; type { TTest } TTest = record  id: Integer;  class operator Initialize(var this: TTest);  class operator Copy(constref src: TTest; var dst: TTest);  class operator AddRef(var this: TTest);  class operator Finalize(var this: TTest);end; { TTest } class operator TTest.Initialize(var this: TTest);begin  this.id := cnt;  Inc(cnt);  WriteLn('Init ', this.id);end; class operator TTest.Copy(constref src: TTest; var dst: TTest);begin  WriteLn('Copy: ', src.id, ' -> ', dst.id);end; class operator TTest.AddRef(var this: TTest);begin  WriteLn('AddRef: ', this.id);end; class operator TTest.Finalize(var this: TTest);begin  WriteLn('Final: ', this.id);end; procedure TestRef(var t: TTest);begin  WriteLn('Ref');end;      procedure TestVal(t: TTest);begin  WriteLn('Val');end; var  t1, t2: TTest;begin  // Expected "Init 0" and "Init 1"  TestRef(t1);  // Expected Ref -> no init, final, addRef or copy needed  TestVal(t2);  // Expected copy followed by addRef and Final in the end  t1 := t2;  // Expected Final followed by copy followed by addRef  // Expected Final *2end.  As annotated, I expect at the begin of the code for the two variables to be initialized -> ok. Then when passing the variable as reference none of the operators should be executed -> ok But then, when the variable is passed by object I don't understand what is happening. It only calls addRef and Final. But isn't this also a copy operation? And the last line is actually confusing me the most. I would expect that 1. final is called on t1 because it gets overwritten, 2. copy would be called to perform the actual copy and 3. AddRef would be called to "finalize the copy". But only copy is called. So some questions about this: 1. Will AddRef only be called on function calls by value? Or to put it differently, why doesn't it get called after the copy? 2. Are copy operations only assignments (i.e. where one initialized object overrides another initialized object)? 3. Why isn't the finalization called before the copy. As I see this, this has the consequence that you always need to implement copy, while if AddRef would always be called after a copy, and Finalize would also be always called before a copy, a lot of management functionality could be implemented without the need to overload the copy operator. But like this the copy operator needs to do both, to handle the AddRef and the Finalization when doing an assignment, and copy would only be needed if a fieldwise copy (i.e. default record assignment) is not possible (i.e. you need a deep copy). Looks to me like this results in a lot of unnecessary code, because in many cases one needs to implement the finalization and addref functionality also in the Copy operator. So what's the rational behind this? ---------------------------------------------------------------------- ASerge · 2020-05-06 23:10 See management operators. Quote1. Will AddRef only be called on function calls by value? Or to put it differently, why doesn't it get called after the copy? Not only, but when FPC not use assignment. "AddRef is called after the contents of a record have been duplicated by a bitwise copy (for example after, not during, an assignment.)" (bold me). Yes, AddRef is called instead of Copy when is passed ByVal for performance reasons. Quote2. Are copy operations only assignments (i.e. where one initialized object overrides another initialized object)? Yes. When completely overwritten. Quote3. Why isn't the finalization called before the copy. It's called after "Finalize is called when a record goes out of scope". Also for performance reasons. In one scope uses the copy behavior. ---------------------------------------------------------------------- jamie · 2020-05-07 02:13 I love it, when we getting something higher than 3.0.4 out there so we can use it ! Those operators look GOOD! ---------------------------------------------------------------------- PascalDragon · 2020-05-07 07:49 Quote from: Warfley on May 06, 2020, 06:13:34 pmLooks to me like this results in a lot of unnecessary code, because in many cases one needs to implement the finalization and addref functionality also in the Copy operator. So what's the rational behind this? You might want to take a look at the announcement mail and the corresponding thread. Maybe that will clear up some bits (I'd have to dive into this myself first). ---------------------------------------------------------------------- Warfley · 2020-05-07 14:14 Ok thank you, this mail clears a lot up about the rational. Haven't thought about supporting calls from outside/interface calls. Quote from: jamie on May 07, 2020, 04:13:17 amI love it, when we getting something higher than 3.0.4 out there so we can use it !Yes, I just installed trunk for this to start playing around and maybe already having some code to use when the next stable version is out. From what I can tell up until now, I'm loving it. ---------------------------------------------------------------------- JiaXing · 2020-05-07 14:20 This advanced record is increasing complex. I used to think it's just normal record but also can contains procedures and functions. It turned out to be a lot more than that. ---------------------------------------------------------------------- PascalDragon · 2020-05-07 14:24 Quote from: Warfley on May 07, 2020, 04:14:47 pmQuote from: jamie on May 07, 2020, 04:13:17 amI love it, when we getting something higher than 3.0.4 out there so we can use it !Yes, I just installed trunk for this to start playing around and maybe already having some code to use when the next stable version is out. From what I can tell up until now, I'm loving it. You could also have installed the 3.2 RC1