Question About Operator Overloading lazarus / 62-General ---------------------------------------------------------------------- ad1mt · 2024-02-10 11:07 Previously, I thought operator overloading had to be done as "methods" inside advanced records (from reading Cantu's "Object Pascal Handbook"). But I recently discovered* that operator overloading can be done independently, as in the following program: 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 {$MODE OBJFPC}{$MODESWITCH ADVANCEDRECORDS}program test_op_overload_1;uses sysutils;typeT_R = record        R_Val:int64;      end; operator :=(const v:int64):T_R;beginResult.R_Val:= v;end; operator +(const v1,v2:T_R):T_R;beginResult.R_Val:= (v1.R_Val + v2.R_Val);end; operator :=(const v:ansistring):int64;beginResult:= strtoint(v);end; beginend.I can see that the visibility of the operator overload functions will be different. But what other advantages/disadvantages are there? I've also realised that operator overloading like this can also be done for built-in types, but are there any disadvantages/warnings about doing this? I have read the Reference Guide, but it does not give advice about details like this. Many thanks for your help. *PS - although I first learned Pascal in about 1982, the language has evolved beyond recognition since, and I did not keep up with those changes, because I never used Pascal at work. ---------------------------------------------------------------------- Thaddy · 2024-02-10 11:34 This is a unique feature of FreePascal, hence Marco Cantu couldn't have known about it    (Actually he probably did know but Delphi does not implement it) It is therefor not Delphi compatible. That is about the only drawback. But maybe they will implement it later. Same like Freepascal had generics long before Delphi. ---------------------------------------------------------------------- ad1mt · 2024-02-10 11:41 Quote from: Thaddy on February 10, 2024, 12:34:34 pmSo although this also works in mode delphi it is not delphi compatible. I don't care about Delphi compatibility. I downloaded a trial a few months ago, and couldn't get it work. So as far as I am concerned, Delphi is a dead duck. It's a bit sad, because I used it in the late 1990's and it was good. ---------------------------------------------------------------------- Thaddy · 2024-02-10 11:45 Instead of the trial you should have downloaded the free version. Anyway if you do not need Delphi compatibility there are few drawbacks. It is possible that such a freestanding operator overload clashes with a non- overloadable system operator. In such case it needs to be implemented as a class or record member. But that is about it. The compiler will throw an impossible overload error in such cases. ---------------------------------------------------------------------- ad1mt · 2024-02-10 12:42 Quote from: Thaddy on February 10, 2024, 12:45:52 pmInstead of the trial you should have downloaded the free version. I did download the free version... I used the wrong word when I said "trial". ---------------------------------------------------------------------- qk · 2024-02-10 12:52 A disadvantage of external operators is that as the number of operators increases, the compilation time grows exponentially. However, I don't think you have such a high number of operators to encounter this issue. Regards qk. ---------------------------------------------------------------------- Paolo · 2024-02-10 13:36 For ovloaded operators like these here shown, I had the impression that there are some penalties in performance wrt direct code calculation, someone here did speed comparisons ? ---------------------------------------------------------------------- Thaddy · 2024-02-10 13:44 Quote from: qk on February 10, 2024, 01:52:51 pmA disadvantage of external operators is that as the number of operators increases, the compilation time grows exponentially. However, I don't think you have such a high number of operators to encounter this issue. Regards qk. I sincerely doubt "exponentially" and can't find any evidence for it either. ---------------------------------------------------------------------- ad1mt · 2024-02-10 13:48 Quote from: qk on February 10, 2024, 01:52:51 pmA disadvantage of external operators is that as the number of operators increases, the compilation time grows exponentially. However, I don't think you have such a high number of operators to encounter this issue. I don't have many operators in the example program. But there are many more in the big integer library I'm working on. At the moment it compiles and runs reasonably fast, but I would not want to slow things down. Thanks. ---------------------------------------------------------------------- Warfley · 2024-02-10 14:00 Class operators have one distinct advantage, they can be used with generic types: 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 type  generic TVector2 = record    x, y: T;    class operator +(const lhs, rhs: specialize TVector2): specialize TVector2;  end; class operator TVector2.+(const lhs, rhs: specialize TVector2): specialize TVector2;begin  Result.x := lhs.x + rhs.x;  Result.y := lhs.y + rhs.y;end; var  v1, v2, v3: specialize TVector2;begin  v1.x := 42;  v1.y := 3.14;  v2.x := 12.5;  v2.y := 67;  v3 := v1 + v2;end; ---------------------------------------------------------------------- qk · 2024-02-10 16:37 Quote from: Thaddy on February 10, 2024, 02:44:50 pmQuote from: qk on February 10, 2024, 01:52:51 pmA disadvantage of external operators is that as the number of operators increases, the compilation time grows exponentially. However, I don't think you have such a high number of operators to encounter this issue. Regards qk. I sincerely doubt "exponentially" and can't find any evidence for it either. What I mean is that compilation time is not proportional to the number of defined operators but grows more rapidly. Additionally, this slowdown has a cascading negative impact on all units that use these operators. Defining operators within records significantly reduces compilation time. This is based on my experience with a unit that contains approximately 1000 operators. Regards ---------------------------------------------------------------------- PascalDragon · 2024-02-12 20:33 Quote from: qk on February 10, 2024, 05:37:14 pmAdditionally, this slowdown has a cascading negative impact on all units that use these operators. Defining operators within records significantly reduces compilation time. This is based on my experience with a unit that contains approximately 1000 operators. That is because the compiler will skip unit's if they contain no (public) operator overloads. If it contains even one operator overload then it needs to look through all the symbols to find a potentially matching operator. For operator overloads inside a record the compiler does not have that problem. (This used to be even worse in the past before I implemented that unit skipping optimization ) ---------------------------------------------------------------------- qk · 2024-02-13 08:37 Quote from: PascalDragon on February 12, 2024, 09:33:30 pmThat is because the compiler will skip unit's if they contain no (public) operator overloads. If it contains even one operator overload then it needs to look through all the symbols to find a potentially matching operator. For operator overloads inside a record the compiler does not have that problem. Thanks  for the explanation, now everything is more clear. Quote from: PascalDragon on February 12, 2024, 09:33:30 pm(This used to be even worse in the past before I implemented that unit skipping optimization ) I assume you're referring to the trunk version; I've noticed it's faster than stable version. Well done! ---------------------------------------------------------------------- PascalDragon · 2024-02-14 20:08 Quote from: qk on February 13, 2024, 09:37:41 amI assume you're referring to the trunk version; I've noticed it's faster than stable version. No, that was a general overload lookup improvement implemented by a third party developer. The fix I'm talking about is already more than ten years old