### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: [question] Management Operator: Copy, AddRef and Finalization  (Read 572 times)

#### Warfley

• Sr. Member
• Posts: 303
##### [question] Management Operator: Copy, AddRef and Finalization
« on: May 06, 2020, 06:13:34 pm »
Hey,
I have a question about management operators.

Take the following example:
Code: Pascal  [Select][+][-]
1. program example;
2.
3. {\$mode delphi}
4.
5. var cnt: Integer;
6.
7. type
8.
9. { TTest }
10.
11. TTest = record
12.   id: Integer;
13.   class operator Initialize(var this: TTest);
14.   class operator Copy(constref src: TTest; var dst: TTest);
15.   class operator AddRef(var this: TTest);
16.   class operator Finalize(var this: TTest);
17. end;
18.
19. { TTest }
20.
21. class operator TTest.Initialize(var this: TTest);
22. begin
23.   this.id := cnt;
24.   Inc(cnt);
25.   WriteLn('Init ', this.id);
26. end;
27.
28. class operator TTest.Copy(constref src: TTest; var dst: TTest);
29. begin
30.   WriteLn('Copy: ', src.id, ' -> ', dst.id);
31. end;
32.
33. class operator TTest.AddRef(var this: TTest);
34. begin
35.   WriteLn('AddRef: ', this.id);
36. end;
37.
38. class operator TTest.Finalize(var this: TTest);
39. begin
40.   WriteLn('Final: ', this.id);
41. end;
42.
43. procedure TestRef(var t: TTest);
44. begin
45.   WriteLn('Ref');
46. end;
47.
48. procedure TestVal(t: TTest);
49. begin
50.   WriteLn('Val');
51. end;
52.
53. var
54.   t1, t2: TTest;
55. begin
56.   // Expected "Init 0" and "Init 1"
57.   TestRef(t1);
58.   // Expected Ref -> no init, final, addRef or copy needed
59.   TestVal(t2);
60.   // Expected copy followed by addRef and Final in the end
61.   t1 := t2;
62.   // Expected Final followed by copy followed by addRef
63.   // Expected Final *2
64. end.
65.

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

• Hero Member
• Posts: 1671
##### Re: [question] Management Operator: Copy, AddRef and Finalization
« Reply #1 on: May 07, 2020, 01:10:43 am »
See management operators.
Quote
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?
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.

Quote
2. Are copy operations only assignments (i.e. where one initialized object overrides another initialized object)?
Yes. When completely overwritten.

Quote
3. 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

• Hero Member
• Posts: 3538
##### Re: [question] Management Operator: Copy, AddRef and Finalization
« Reply #2 on: May 07, 2020, 04:13:17 am »
I love it, when we getting something higher than 3.0.4 out there so we can use it !

Those operators look GOOD!
The only true wisdom is knowing you know nothing

#### PascalDragon

• Hero Member
• Posts: 2138
• Compiler Developer
##### Re: [question] Management Operator: Copy, AddRef and Finalization
« Reply #3 on: May 07, 2020, 09:49:30 am »
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?

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

• Sr. Member
• Posts: 303
##### Re: [question] Management Operator: Copy, AddRef and Finalization
« Reply #4 on: May 07, 2020, 04:14:47 pm »
Ok thank you, this mail clears a lot up about the rational. Haven't thought about supporting calls from outside/interface calls.

I 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

• Jr. Member
• Posts: 75
##### Re: [question] Management Operator: Copy, AddRef and Finalization
« Reply #5 on: May 07, 2020, 04:20:18 pm »
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.
I'm subscribed to the church of 440bx. Say no to OOP

#### PascalDragon

• Hero Member
• Posts: 2138
• Compiler Developer
##### Re: [question] Management Operator: Copy, AddRef and Finalization
« Reply #6 on: May 07, 2020, 04:24:58 pm »
I 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