有没有办法在Delphi中基于不同的Result类型来重载一个函数? [英] Is there a way to overload a function based on different Result type in Delphi?

查看:299
本文介绍了有没有办法在Delphi中基于不同的Result类型来重载一个函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

功能按返回类型重载?

对返回类型的函数重载的理性有一个非常详细的答案,从我可以看到,Delphi不允许这样做,但是是否有任何解决方法来重载基于Delphi中不同返回类型的函数?

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?

推荐答案

记录的隐式和显式转换操作符允许以返回类型重载:即正在转换为的类型:


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;

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

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);

这篇关于有没有办法在Delphi中基于不同的Result类型来重载一个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆