【发布时间】:2011-12-05 00:22:38
【问题描述】:
我有一个后续问题,即存在带参数和不带参数的重载方法定义会导致编译错误,这里已经讨论过:Why is this reference ambiguous?
回顾一下:
trait A {
def foo( s: String ) : String
def foo : String = foo( "foo" )
}
object B extends A {
def foo( s: String ) : String = s
}
B.foo // won't compile
导致错误信息:
error: ambiguous reference to overloaded function
both method foo in object B of type(s: String)String
and method foo in trait A of type => String
match expected type Unit
B.foo
一个可行的解决方案是为编译器提供预期的类型,如下所示:
val s: String = B.foo
不幸的是,人们可能并不总是想要引入额外的变量(例如在断言中)。在上面引用的早期文章的答案中至少推荐两次的解决方案之一是调用带有空括号的方法,如下所示:
B.foo()
不幸的是,这会导致类似的编译器错误(Scala 2.9.0.1):
(s: String)String <and>
=> String
cannot be applied to ()
B.foo()
这是一个错误,还是推荐的解决方案有误? 最终:有哪些选择可以简洁地做到这一点,例如:
assert( B.foo == "whatever" )
而不是
val expected : String = B.foo
assert( expected == "whatever" )
谢谢。
【问题讨论】:
-
B无法编译,因为您忘记在 traitA中提供def foo: String的实现。 -
@agilesteel 对,A 上无参数 foo 的实现在传输中丢失了。现在解决了。错误保持不变。
标签: scala overloading ambiguity