不同编程语言中的重载
有人可以解释(举例)上下文无关重载和上下文相关重载之间的区别吗?
Can somebody please explain (with example) the difference between context-independent and context-dependent overloading?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我从来没有听说过这些。谷歌上只有大约五次点击,其中之一就是这个问题,这似乎表明这些都是虚构的术语。与任何虚构的术语一样,如果你想知道它的含义,你必须询问创造它的人。
据我所知,它似乎与基于返回类型的重载有关。
基本上,如果您有四个像这样的重载函数:
这样调用它们:
然后,使用上下文相关的重载(即基于返回类型和参数类型的重载),将选择以下实现:
并且您 与上下文无关的重载(即忽略返回类型),将选择以下实现:
在两种
ERROR
情况下,foo :: string → int
之间存在歧义、foo :: string → string
和foo :: string → ()
,因为它们仅在返回类型上有所不同,但具有相同的参数类型。I have never heard about those. And there's only about five hits on Google, one of which is this very question, which seems to suggest to me that these are made-up terms. And as with any made-up term, if you want to know what it means, you have to ask the person who made it up.
From what little I could gather, it seems to be related to return-type based overloading.
Basically, if you have four overloaded functions like these:
And you call them like this:
Then, with context-dependent overloading (i.e. overloading based on the return type as well as the parameter types), the following implementations will be selected:
Whereas with context-independent overloading (i.e. ignoring the return type), the following implementations will be selected:
In both the
ERROR
cases, there is an ambiguity betweenfoo :: string → int
,foo :: string → string
andfoo :: string → ()
, since they only differ in their return type but have the same paremeter type.引用自此处:
Quoting from here: