views:

186

answers:

2

http://stackoverflow.com/questions/442026/function-overloading-by-return-type

has a very detailed answer on the rational on function overloading by return type, and from what I can see Delphi does not allow this, but are there any workarounds to overload a function based on different return type in Delphi?

+6  A: 

You can take the "result" as a parameter.

procedure Blah( InVar : word; out OutVar : Byte ); overload;
procedure Blah( InVar : word; out OutVar : String ); overload;
Kornel Kisielewicz
Yep, I could do this, but I am updating a component, and the method is currently implemented as a function, so if I changed to a procedure call I would have to go back through all the code calling it.
HMcG
+6  A: 

The implicit and explicit conversion operators for records permit overloading by return type: namely, the type being converted to:

type
  TFoo = record
    class operator Implicit(const AFoo: TFoo): Integer;
    class operator Implicit(const AFoo: TFoo): string;
  end;

Depending on the context, using a value of type TFoo will call the appropriate implicit conversion. If trying to use a value of type TFoo as an argument to an overloaded routine that can take either Integer or string in that position, an overload error will occur:

test.pas(33) Error: E2251 Ambiguous overloaded call to 'Q'
 + Test.pas(19) Related method: procedure Q(Integer);
 + Test.pas(24) Related method: procedure Q(const string);
Barry Kelly
Ah well, not quite the answer I was hoping for, but interesting anyway. Cheers.
HMcG