Operator overloading: Someone please explain this lazarus / 62-General ---------------------------------------------------------------------- Zvoni · 2019-03-18 20:07 Hi Folks, playing around i overloaded some operators 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 //Somewhere in a Unitinterface//String and LongIntOperator +(AString:String;ALongInt:LongInt):String;Overload;Operator +(ALongInt:LongInt;AString:String):String;Overload;                   ImplementationOperator +(AString:String;ALongInt:LongInt):String;Begin  Result:=AString+ALongInt.ToString;end;  Operator +(ALongInt:LongInt;AString:String):String;Begin  Result:=ALongInt.ToString+AString;end;       Using above i stumbled across this: 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 Uses MyOperatorUnit;VarMyText:String;MyNumber:Integer;        BeginMyText:='Test ';MyNumber:=123456;       Writeln(MyText+MyNumber); //WorksWriteln(MyNumber+MyText); //WorksWriteln(MyNumber+' Test'); //WorksWriteln(123456+MyText);  //WorksWriteln(MyText+123456);   //WorksWriteln('Test '+MyNumber); //Doesn't work?!?!??! --> project1.lpr(19,19) Error: Incompatible types: got "LongInt" expected "AnsiString"   Huh? I'm looking at it, and it doesn't make any sense to me. ---------------------------------------------------------------------- ASerge · 2019-03-18 20:25 Quote from: Zvoni on March 18, 2019, 09:07:57 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Writeln('Test '+MyNumber); //Doesn't work?!?!??! --> project1.lpr(19,19) Error: Incompatible types: got "LongInt" expected "AnsiString"Huh? I'm looking at it, and it doesn't make any sense to me. A quoted string can be string, UnicodeString, ShortString, PChar, PWideChar and so on. Cast it: string('Test ') +... ---------------------------------------------------------------------- Thaddy · 2019-03-18 20:35 The real explanation is adding {$H+} ---------------------------------------------------------------------- ASerge · 2019-03-18 20:40 Quote from: Thaddy on March 18, 2019, 09:35:35 pmThe real explanation is adding {$H+} Check before you advise. The error occurs in {$H+} 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}{$APPTYPE CONSOLE}{$LONGSTRINGS ON} uses SysUtils; operator + (const Left: string; Right: LongInt): string;begin  Result := Left + IntToStr(Right);end; operator + (Left: LongInt; const Right: string): string;begin  Result := IntToStr(Left) + Right;end; var  Text: string = 'text';  Number: LongInt = 123456;begin  Writeln(Text + Number);  Writeln(Number + Text);  Writeln(Number + 'other text');  Writeln(789012 + Text);  Writeln(Text + 789012);  Writeln('else text' + Number); // project1.lpr(26,25) Error: Incompatible types: got "LongInt" expected "AnsiString"  Readln;end. ---------------------------------------------------------------------- Zvoni · 2019-03-18 21:37 So what's the Explanation/Solution? You lost me along the way there.... ---------------------------------------------------------------------- howardpc · 2019-03-18 21:51 ASerge already explained. A string constant could be, for example, a PChar. Because of the plethora of string types FPC supports, the compiler cannot know unambiguously what the type of 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  'some string constant' might be. Casting the constant to a String, allows the compiler to see that it is indeed compatible with the String declaration of the parameter in your overloaded operator function. ---------------------------------------------------------------------- Zvoni · 2019-03-18 21:59 Quote from: howardpc on March 18, 2019, 10:51:28 pmASerge already explained. A string constant could be, for example, a PChar. Because of the plethora of string types FPC supports, the compiler cannot know unambiguously what the type of 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  'some string constant' might be. Casting the constant to a String, allows the compiler to see that it is indeed compatible with the String declaration of the parameter in your overloaded operator function. ???? Then the Error-Message doesn't make sense one jot at all. I was looking at the Integer the whole time, while my String-Literal was at fault. The Error marked the Integer-Variable to boot! And it doesn't explain why my Line 11/Serge's Line23 works..... Or is it because that very first argument is that ambiguous Literal? Because going by that logic, something like Writeln('Text1 '+' Text2'); shouldn't work, which it does; Oh, and FWIW: I stumbled across that not by using Writeln, but passing a String to a Function/Procedure which expects a String ---------------------------------------------------------------------- Zvoni · 2019-03-18 22:13 After fiddling around a bit more, i'm still not satisfied with the proposed Answer! For grins i did a 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 Varb:Boolean; BeginWriteln('Test '+b);  How is it, that the compiler throws the error project1.lpr(7,18) Error: Operator is not overloaded: "Constant String" + "Boolean" Constant String against Boolean. So, the compiler recognizes it as a Constant String. If i overload the "+"-Operator in a String/Boolean and Boolean/String- Combination, all combinations pass, even those with Literals Writeln('This '+True);                  //Works! EDIT: Everything in {$mode objfpc}{$H+}          EDIT: OH NONONO I Reject the Answer. I just did the same Test as in my first Post, but with Double instead of Integer. It worked in all combinations! So no, i don't believe it's the "Constant string" and the Error- Message is correct. It's the Integer! EDIT3: There is something wrong. I just commented out my operator unit (to force error-messages) In the combination in question it's still the same error-message, so the error occurs before checking if an (overloaded) operator is available ---------------------------------------------------------------------- howardpc · 2019-03-19 00:26 You are correct. The compiler appears to treat a constant string in combination with other types differently if the other type is any kind of integer. It seems to get stuck at its own internal concatenation attempt, and fails to consider the correctly declared overloaded operator alternative. ---------------------------------------------------------------------- Zvoni · 2019-03-19 05:33 Quote from: howardpc on March 19, 2019, 01:26:21 amYou are correct. The compiler appears to treat a constant string in combination with other types differently if the other type is any kind of integer. It seems to get stuck at its own internal concatenation attempt, and fails to consider the correctly declared overloaded operator alternative. Conclusion: Bug? Considering a boolean (see above) is basically an integer ---------------------------------------------------------------------- Thaddy · 2019-03-19 08:25 Yes, it is a bug, and for all supported string types the same bug. Same happens in shortstring, ansistring and unicodestring modes. Just to be sure I also tested if sysutils could have some influence (because it contains some overloads?), but that is not the case. Here's my reduced 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 {$MODE OBJFPC}{$IFDEF MSWINDOWS}{$APPTYPE CONSOLE}{$ENDIF} operator + (Left: LongInt; const Right: string): string;begin  writestr(Result,Left,Right);end;  operator + (const Left: string; Right: LongInt): string;begin  writestr(Result,Left,Right);end;  var  Text: string = 'text';  Number: LongInt = 123456;begin  Writeln(Text + Number); // V+V  Writeln(Number + Text); // V+V  Writeln(789012 + Text);//L+V  Writeln(Text+789012);//V+L  // Next fails and is L+V  Writeln('else text'+Number); // fails only on LEFT string literals + non-literal number with all string types  // Next pass again  Writeln(Number + 'other text'); // passes, V+L  Writeln(string('else text')+Number); //passes, cast(L)+V  writeln(789012 + 'other text'); // passes, L+L  writeln('other text',789012); // passes, L+L  Readln;  Readln;end.As you can see I have added some more - missing- tests from which one can conclude that this is the exact issue: The overloaded operator fails only on LEFT string literals + non- literal number with all string types. All other possible cases pass. ---------------------------------------------------------------------- Zvoni · 2019-03-19 10:35 Someone going to report that bug? I've never done that  EDIT: Thaddy, your last Test is not of the same variety. You use Writeln String-Literal COMMA LiteralInteger. You're passing 2 Arguments to Writeln instead of one writeln('Test'+123456);    This one fails too with the same errormessage ---------------------------------------------------------------------- Thaddy · 2019-03-19 10:37 I will do that now. ---------------------------------------------------------------------- Zvoni · 2019-03-19 10:40 See my edit ---------------------------------------------------------------------- Thaddy · 2019-03-19 11:17 Mm. call it a typo. Corrected.