Recent

Author Topic: {solved} Is it possible to overload functions to have different result types  (Read 7064 times)

Kraig

  • New Member
  • *
  • Posts: 28
I want to use same name for functions that return different types
Something along the lines of
Code: Pascal  [Select][+][-]
  1. Function x:string;
  2. Function x:integer;
  3.  

Whereby the type of the variable being assigned the result of function would determine which function is called.

Is this possible? So far I cannot get it to work even with functions of different result types.
Although the functions with same name compile ok it seems to try to use the function x:string when I try to assign integervariable:= x
« Last Edit: October 21, 2023, 04:48:53 pm by Kraig »

Kays

  • Hero Member
  • *****
  • Posts: 632
  • Whasup!?
    • KaiBurghardt.de
Re: Is it possible to overload functions to have different result types
« Reply #1 on: October 19, 2023, 08:05:34 am »
I 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][+][-]
  1. {$mode objFPC}
  2. program overlord(input, output, stdErr);
  3.         type
  4.                 dummy = record end;
  5.        
  6.         operator := (x: dummy): string;
  7.                 begin
  8.                         result := 'Foo';
  9.                 end;
  10.        
  11.         operator := (x: dummy): integer;
  12.                 begin
  13.                         result := 42;
  14.                 end;
  15.        
  16.         { === MAIN ==================== }
  17.         var
  18.                 tmp: dummy;
  19.                 str: string;
  20.                 int: integer;
  21.         begin
  22.                 str := tmp; { assigns 'Foo' }
  23.                 int := tmp; { assigns 42 }
  24.         end.
Yours Sincerely
Kai Burghardt

Zvoni

  • Hero Member
  • *****
  • Posts: 3377
Re: Is it possible to overload functions to have different result types
« Reply #2 on: October 19, 2023, 08:47:59 am »
I want to use same name for functions that return different types
Something along the lines of
Code: Pascal  [Select][+][-]
  1. Function x:string;
  2. Function x:integer;
  3.  

Whereby the type of the variable being assigned the result of function would determine which function is called.

Is this possible? So far I cannot get it to work even with functions of different result types.
Although the functions with same name compile ok it seems to try to use the function x:string when I try to assign integervariable:= x
As Kays said: Not possible.

Rework it to a Procedure
Code: Pascal  [Select][+][-]
  1. Procedure x(out i:Integer);Overload;
  2. Procedure x(out s:String);Overload;
  3.  
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Warfley

  • Hero Member
  • *****
  • Posts: 2058
Re: Is it possible to overload functions to have different result types
« Reply #3 on: October 19, 2023, 11:35:50 am »
Note, if you where inclined to do so, you could use a transparent Return type which would call the respective function on the implicit cast:
Code: Pascal  [Select][+][-]
  1. type
  2.   TXResult=record
  3.     // all the parameters required for the function
  4.     // in this example none
  5.   end;
  6.  
  7. function x: TXresult;
  8. Begin
  9.   //Copy parameters into result
  10.   // in this example no parameters
  11. End;
  12.  
  13. operator :=(XR: TXresult): String;
  14. Begin
  15.   // call string overloaded version
  16.   Result:=x_string();
  17. end;
  18.  
  19. operator :=(XR: TXresult): Integer;
  20. Begin
  21.   // call Integer overloaded version
  22.   Result:=x_integer();
  23. end;

Which you could then use transparently:
Code: Pascal  [Select][+][-]
  1. var
  2.   S: String;
  3.   I: Integer;
  4. begin
  5.   S:=x; // returns x result which will be casted to string calling the string overload
  6.   I:=x; // returns x result which will be casted to Integer calling the Integer overload

Kraig

  • New Member
  • *
  • Posts: 28
Re: Is it possible to overload functions to have different result types
« Reply #4 on: October 19, 2023, 01:12:21 pm »
Thanks for the replies,
 I’m a bit confused about the syntax of the solutions.

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Is it possible to overload functions to have different result types
« Reply #5 on: October 19, 2023, 01:22:39 pm »
I’m a bit confused about the syntax of the solutions.
It is a fairly new feature called operator (overloading).

The wiki and the manual both write about it. You should be looking for the topic assignment operator(s).

At least, I suspect, that is the part that is confusing for you ?
Today is tomorrow's yesterday.

avra

  • Hero Member
  • *****
  • Posts: 2586
    • Additional info
Re: Is it possible to overload functions to have different result types
« Reply #6 on: October 19, 2023, 02:05:21 pm »
I want to use same name for functions that return different types
Couldn't you simply use variant for a return data type?
https://wiki.freepascal.org/Variant
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

Bart

  • Hero Member
  • *****
  • Posts: 5721
    • Bart en Mariska's Webstek
Re: Is it possible to overload functions to have different result types
« Reply #7 on: October 19, 2023, 03:05:38 pm »
I want to use same name for functions that return different types
Something along the lines of
Code: Pascal  [Select][+][-]
  1. Function x:string;
  2. Function x:integer;
  3.  

You cannot, and for a good reason.
What should the compiler do if you do
Code: Pascal  [Select][+][-]
  1. function x: string;
  2. begin
  3.   Result := 'foo';
  4. end;
  5. function x: integer;
  6. begin
  7.   Result:= 666;
  8. end;
  9.  
  10. begin
  11.   writeln('X = ',x);  //should it write 'foo' or 666??
  12. end.
The compiler cannot distinguish between the 2 functions in such a case.

So, you have to work around this, as other have suggested.

Bart

Kraig

  • New Member
  • *
  • Posts: 28
Re: Is it possible to overload functions to have different result types
« Reply #8 on: October 19, 2023, 03:13:52 pm »
Quote
. At least, I suspect, that is the part that is confusing for you ?

My understanding of operator overloading is like making  := behave like a procedure name? But I guess it doesn’t interfere with that operator used in other places? So it’s sort of like making an overloaded procedure named :=  ?

The variants are also an interesting idea, I’ve never used those before.

Bart good point on the calling function directly rather than assigning to a variable. The writeln needs to know the type of what it’s going to write ?

Warfley

  • Hero Member
  • *****
  • Posts: 2058
Re: Is it possible to overload functions to have different result types
« Reply #9 on: October 19, 2023, 03:36:15 pm »
The operator := is, contrary to intuition not overloading the actual assignment operator, but the implicit and explicit cast.

So when you have the operator defined as
Code: Pascal  [Select][+][-]
  1. operator:=(SRC: TSource): TDest;
This operator will always be called when trying to (automatically) convert a TSource to TDest.

Example:
Code: Pascal  [Select][+][-]
  1. operator:=(I:Integer): String;
  2. begin
  3.   Result := IntToStr(I);
  4. end;
  5.  
  6. ...
  7.  
  8. ShowMessage(42); // because string is expected and there is a cast operator for int to string, 42 will be automatically converted to string

Zvoni

  • Hero Member
  • *****
  • Posts: 3377
Re: Is it possible to overload functions to have different result types
« Reply #10 on: October 19, 2023, 03:36:41 pm »
*snip* The writeln needs to know the type of what it’s going to write ?
Not exactly.
The Writeln-Procedure is "in a way" overloaded itself for incoming Parameters, because it accepts Strings, Integers, Doubles etc.
(I think "Array Of Const" or some such)

So in your case: at compile-time Writeln has no chance to know which one of the "x"-Function to call, since both are valid "types"
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Warfley

  • Hero Member
  • *****
  • Posts: 2058
Re: Is it possible to overload functions to have different result types
« Reply #11 on: October 19, 2023, 04:13:02 pm »
(I think "Array Of Const" or some such)

No the read and write family of functions (as well as some others) are not functions at all and don't follow any rule for function parameters. They are just compiler magic. There is no way, not with array of const, nor with generics or any other in language feature of Pascal to write a function that behaved like read or write(ln)

So write is here a very specific example.

A more common example why result type overloading doesn't work is simply that (at least in Delphi and mode ObjFPC) you do not need to fetch the result of a function
Code: Pascal  [Select][+][-]
  1. function x: Integer;
  2. ...
  3. function x: String;
  4. ...
  5.  
  6. begin
  7.   x(); // what to do here?
  8. end;

This is also why the assignment/implicit cast operator works, because it will only ever be called when a conversion must take place, in which case there is a clear target type. That said, if you have something that may convert to two types and you have an overloaded function that takes these types as parameters, there is still the problem of disambiguation. And while I can't test right now, I believe this will simply throw an error and require the user to use an explicit cast
« Last Edit: October 19, 2023, 04:18:31 pm by Warfley »

Zvoni

  • Hero Member
  • *****
  • Posts: 3377
Re: Is it possible to overload functions to have different result types
« Reply #12 on: October 19, 2023, 04:19:29 pm »
No the read and write family of functions (as well as some others) are not functions at all and don't follow any rule for function parameters. They are just compiler magic. There is no way, not with array of const, nor with generics or any other in language feature of Pascal to write a function that behaved like read or write(ln)
I'll take your word for it.

OTOH, if i remember correctly, in assembly a Function is still a "procedure" with the "difference" being that the result-value is passed as the "last" parameter as an "out"-param (in a special (?) register)
(i don't know the "words" to describe it better)
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

jamie

  • Hero Member
  • *****
  • Posts: 7707
Re: Is it possible to overload functions to have different result types
« Reply #13 on: October 20, 2023, 03:00:27 am »
Write a single Function X:Integer;

if you want a string from that whenever needed inline.

MyString := X.Tostring;

make sure the sysutils are in the uses list.

Done.

The only true wisdom is knowing you know nothing

Zvoni

  • Hero Member
  • *****
  • Posts: 3377
Re: Is it possible to overload functions to have different result types
« Reply #14 on: October 20, 2023, 11:58:51 am »
Write a single Function X:Integer;

if you want a string from that whenever needed inline.

MyString := X.Tostring;

make sure the sysutils are in the uses list.

Done.
That's got nothing to do with OP's scenario.

He wants to define one Function-Name (!) in an overloaded fashion, fopr him to be able to do:
Code: Pascal  [Select][+][-]
  1. intI:=x; //Returns 42
  2. s:=x; //Returns 'Hello World'
Your proposal still only accepts Integers
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

 

TinyPortal © 2005-2018