Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to overload a function based on different Result type in Delphi?

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?

like image 331
HMcG Avatar asked Jan 23 '10 21:01

HMcG


1 Answers

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);
like image 53
Barry Kelly Avatar answered Nov 15 '22 08:11

Barry Kelly