为什么 Scala 在使用按名称参数重载的情况下的行为与使用按值参数重载的情况不同?
给定以下 Scala 代码:
object test {
def byval(a: Int) = println("Int")
def byval(a: Long) = println("Long")
def byname(a: => Int) = println("=> Int")
def byname(a: => Long) = println("=> Long")
def main(args: Array[String]) {
byval(5)
byname(5)
}
}
调用 byval(5) 可以正确编译,但 byname 无法编译:
ambiguous reference to overloaded definition
为什么?我希望在重载方面观察到按值和按名称参数的相同行为……如何修复?
Given this Scala code:
object test {
def byval(a: Int) = println("Int")
def byval(a: Long) = println("Long")
def byname(a: => Int) = println("=> Int")
def byname(a: => Long) = println("=> Long")
def main(args: Array[String]) {
byval(5)
byname(5)
}
}
the call byval(5) compiles correctly, but byname fails to compile:
ambiguous reference to overloaded definition
Why? I would expect to observe the same behavior for by-value and by-name parameters with respect to overloading… How can it be fixed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您不想使用不同的方法名称,则无需重载的可能解决方法(除了前面所说的之外):
Possible workaround without overloading (in addition to what has been said earlier), if you don't want to use different method names:
这是因为 JVM 不支持“按名称”参数,因此 Scala 必须以另一种方式实现它。 <代码> => X 实际上编译为
Function0[X]
,它会擦除为Function0[Object]
,这使得 Scala 无法区分仅在以下方面有所不同的两个方法:按名称参数的预期类型。That's because JVM does not support a "by-name" parameter, so Scala has to implement it in another way.
=> X
actually compiles to aFunction0[X]
, which erases toFunction0[Object]
, which makes it impossible for Scala to distinguish two methods that differ only by the expected type of a by-name parameter.