我在看FoldLeft和FoldRight方法,并且该方法的操作符版本非常奇特,就像这样(0 /:List.range(1,10))(+)。 对于具有两个参数列表的右联合函数,人们可能会认为语法是这样的(HostClass((param1)(param2))。 但在这种情况下,它是语法(param1 op HostClass)(param2)。这会导致另一种情况的歧义,即右联合函数返回另一个接受单个参数的函数。 由于这种不明确性,类会编译,但在进行函数调用时会失败,如下所示。正确的关联函数与两个参数列表
class Test() {
val func1:(String => String) = { (in) => in * 2 }
def `test:`(x:String) = { println(x); func1 }
def `test:`(x:String)(y:String) = { x+" "+y }
}
val test = new Test
(("Foo") `test:` test)("hello")
<console>:10: error: ambiguous reference to overloaded definition,
both method test: in class Test of type (x: String)(y: String)String
and method test: in class Test of type (x: String)String => String
match argument types (String)
(("Foo") `test:` test)("hello")
所以我的问题是
这是一个预期的行为或者是一个错误吗?
为什么这两个参数列表正确的关联函数调用已经被设计出来了,而不是我认为更直观的语法((param1)(param2)op HostClass)?
是否有一种解决方法来调用重载测试:函数中没有任何歧义。
试''DEF测试:'(X:字符串,Y:字符串)= {X +”“+ Y}' – 2015-03-31 08:16:06