Lui*_*hys 5 scala infix-notation currying partial-application associativity
换句话说,有没有一个很好的理由为什么不应该编译?
def f(xs: List[Int]) = xs.foldLeft(0) _ // OK
def f(xs: List[Int]) = (xs :\ 0) _ // OK
def f(xs: List[Int]) = (0 /: xs) _
<console>:15: error: missing arguments for method /: in trait TraversableOnce;
follow this method with `_' if you want to treat it as a partially applied function
Run Code Online (Sandbox Code Playgroud)
以下是一些解决方法:
def f(xs: List[Int]) = xs./:(0) _
def f(xs: List[Int]): ((Int, Int) => Int) => Int = (0 /: xs)
Run Code Online (Sandbox Code Playgroud)
但我的问题主要是关于一般的正确语法.
我刚刚修复了这个问题,但我还无法签入它,因为它需要修改规范。
scala> def f(xs: List[Int]) = (0 /: xs) _
f: (xs: List[Int])(Int, Int) => Int => Int
scala> f(1 to 10 toList)
res0: (Int, Int) => Int => Int = <function1>
scala> res0(_ + _)
res1: Int = 55
Run Code Online (Sandbox Code Playgroud)
问题是,如果 op 是右关联的,则规范定义“e1 op e2”为 { val x=e1; e2.op(x ) } 的原因对我来说并不明显,因为更简单的 e2.op(e1) 解决了这个问题,例如https://issues.scala-lang.org/browse/SI-1980。我会进行询问。
| 归档时间: |
|
| 查看次数: |
910 次 |
| 最近记录: |