【发布时间】:2015-03-31 05:09:53
【问题描述】:
我正在查看 FoldLeft 和 FoldRight 方法,该方法的操作符版本非常奇特,类似于 (0 /: List.range(1,10))(+) . 对于具有两个参数列表的右关联函数,人们会期望语法类似于 this((param1)(param2) op HostClass)。 但在这种情况下,它的语法是 (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) 语法?
是否有一种解决方法可以毫无歧义地调用任一重载测试:函数。
【问题讨论】:
-
试试
deftest:(x:String, y:String) = { x+" "+y }
标签: scala