function overloading feature request lazarus / 64-FPC development ---------------------------------------------------------------------- ad1mt · 2023-12-31 12:56 I've got a function overloading feature request. I need the compiler to be able to resolve overloaded functions based on the return type, in addition to the parameter types. So that the following functions would be syntactically valid, and be resolved correctly: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var R:real; I:integer;function F(p1:integer):integer;overload;  begin {version 1} end;function F(p1:integer):real;overload;  begin {version 2} end;beginR:=F(99);  //  <--- this calls version 1 I:=F(99);  //  <--- this calls version 2I'm creating on a library that provides a new data type, and this feature would be very useful. Would this be possible? ---------------------------------------------------------------------- wp · 2023-12-31 13:07 Quote from: ad1mt on December 31, 2023, 01:56:27 pmWould this be possible? I'm afraid: no. Because a function can be called like a procedure without assigning its return value to a variable. And if you call the integer version of the function you can assign its result to a floating point variable due to internal conversion by the compiler. In both cases, there is no way for the compiler to distinguish which overload should be used. ---------------------------------------------------------------------- Josh · 2023-12-31 13:27 my 2cents.. if you had a complex computational line where your result is a real but your using a mix of real/integer versionof the functions in the calculation it would either not compile or give spurious result that could take an age to track down unless you were to cast the function to use the correct one; it would be simpler to have the p1 variable set to the datatype,much easier to debug, and will generate appropriate compiler error if you have something wrong. not sure what it would doto existing code. 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 function F(p1:longint):longint;overload;begin {version 1}  writeln('v1');  result:=p1 div 2;end; function F(p1:Double):Double;overload;begin {version 2}  writeln('v2');  result:=p1/2.0;end; begin  writeln(f(99));  writeln(f(99.0):2:2);  readln;end.                 ps Happy New Year ---------------------------------------------------------------------- MarkMLl · 2023-12-31 13:45 See https://forum.lazarus.freepascal.org/index.php/topic,60218.msg4505 10.html#msg450510 from one of the core developers. There's more automatic conversion and less sensitivity to the lvalue type than I for one am happy with: even if the developers agreed we're pretty much stuck with it. MarkMLl ---------------------------------------------------------------------- Paolo · 2023-12-31 14:09 but the proposed code compiles 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 var R:real; I:integer;function F(p1:integer):integer;overload;  begin {version 1} end;function F(p1:integer):real;overload;  begin {version 2} end;beginR:=F(99);  //  <--- this calls version 1I:=F(99);  //  <--- this calls version 1 tooend;  while this not, and just swapped the order of  listed function 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 var R:real; I:integer;function F(p1:integer):real;overload;  begin {version 2} end;function F(p1:integer):integer;overload;  begin {version 1} end;beginR:=F(99);  //  <--- this calls version 2I:=F(99);  //  <--- this calls version 2 too and as consequence cannot be compiledend;  no compiler hint/warning for such situation ? just kept the first declaration encontered in the code ? ---------------------------------------------------------------------- Leledumbo · 2024-01-01 09:29 Quote from: Paolo on December 31, 2023, 03:09:38 pmbut the proposed code compiles Not anymore in the latest trunk, that was an obvious compiler bug. Both versions are now rejected. ---------------------------------------------------------------------- Paolo · 2024-01-01 12:05 Ok ---------------------------------------------------------------------- PascalDragon · 2024-01-02 22:44 Quote from: ad1mt on December 31, 2023, 01:56:27 pmI'm creating on a library that provides a new data type, and this feature would be very useful. Would this be possible? No, this would not be possible. However if you're working on a custom type anyway then you can declare assignment operator overloads which are essentially the only exception where this is allowed (cause otherwise they wouldn't make sense). ---------------------------------------------------------------------- Kays · 2024-01-03 12:45 Quote from: ad1mt on December 31, 2023, 01:56:27 pm[…] able to resolve overloaded functions based on the return type, in addition to the parameter types. […]If it’s just unary functions:Quote from: Kays on October 19, 2023, 08:05:34 amQuote from: Kraig on October 19, 2023, 07:31:06 amI want to use same name for functions that return different types […] Is this possible? […]No, this is not possible; except for operator overloading you can “select” the := function by its result data type.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}program overlord(input, output, stdErr);        type              dummy = record end;                operator := (x: dummy): string;                begin                        result := 'Foo';              end;                operator := (x: dummy): integer;              begin                        result := 42;                end;                { === MAIN ==================== }        var              tmp: dummy;                str: string;                int: integer;        begin                str := tmp; { assigns 'Foo' }              int := tmp; { assigns 42 }        end. ---------------------------------------------------------------------- jcmontherock · 2024-01-03 21:55 Did you try the "out" parameter in the 2 functions definition to be overriding ?