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

HMc*_*McG 6 delphi overloading function

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?

Bar*_*lly 9

记录的隐式和显式转换运算符允许通过返回类型进行重载:即,转换为的类型:

type
  TFoo = record
    class operator Implicit(const AFoo: TFoo): Integer;
    class operator Implicit(const AFoo: TFoo): string;
  end;
Run Code Online (Sandbox Code Playgroud)

根据上下文,使用type值TFoo将调用适当的隐式转换.如果尝试使用type值TFoo作为重载例程的参数,该例程可以在该位置采用Integer或字符串,则会发生重载错误:

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);
Run Code Online (Sandbox Code Playgroud)


Kor*_*icz 7

您可以将"结果"作为参数.

procedure Blah( InVar : word; out OutVar : Byte ); overload;
procedure Blah( InVar : word; out OutVar : String ); overload;
Run Code Online (Sandbox Code Playgroud)