具有反向关联中缀表示法的柯里化函数的部分应用语法
换句话说,是否有充分的理由不能编译?
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
这里有一些解决方法:
def f(xs: List[Int]) = xs./:(0) _
def f(xs: List[Int]): ((Int, Int) => Int) => Int = (0 /: xs)
但我的问题主要是关于一般正确的语法。
In other words, is there a good reason why this shouldn't compile?
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
Here are some workarounds:
def f(xs: List[Int]) = xs./:(0) _
def f(xs: List[Int]): ((Int, Int) => Int) => Int = (0 /: xs)
but my question is mainly about the proper syntax in general.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我刚刚修复了这个问题,但我还无法签入它,因为它需要修改规范。
问题是,如果 op 是右关联的,则规范定义“e1 op e2”为 { val x=e1; e2.op(x ) } 的原因对我来说并不明显,因为更简单的 e2.op(e1) 解决了这个问题等,例如 https://issues.scala-lang.org/browse/SI-1980。我会进行询问。
I fixed this just now, but I can't check it in yet because it requires amending the specification.
The problem is that the spec defines "e1 op e2" if op is right-associative to be { val x=e1; e2.op(x ) } for reasons which are not apparent to me, since the simpler e2.op(e1) solves this issue among others, like https://issues.scala-lang.org/browse/SI-1980. I will make inquiries.
看起来像一个编译器错误。我已经在不同的 scala 版本和我得到的结果上测试了这个表达式:
它对于
2.9.1.final
和2.8.2.final
的行为是相同的,但是对于2.7.7.final
它会触发不同的错误消息(Iterable
与TraversableOnes
),但我认为这是因为旧版本中集合库的重新设计。我在评论中提到的表达式对于不同的 scala 版本表现不同。
scala 2.9.1.final:
确实令人困惑的编译器消息,绝对是一个错误。
scala 2.8.2.final:
一开始就很奇怪
=>
,与 2.7.7.final 结果相比,看起来像是回归。scala 2.7.7.final:
found
看起来是正确的,但代码仍然无法工作。我在 scala bugtracker 上搜索了类似的问题,但找不到任何合适的内容。认为创建一个票证就足够了(或两个?看起来这两个错误不相关)。
Looks like a compiler bug. I've tested this expressions on different scala versions and what I've got for:
It behaves the same for
2.9.1.final
and2.8.2.final
but for2.7.7.final
it fires different error message (Iterable
vs.TraversableOnes
), but I think it's because of collections library redesign in older versions.Expression, that I mentioned in comment behaves different for different scala versions.
scala 2.9.1.final:
Really confusing compiler message, definitely a bug.
scala 2.8.2.final:
Weird
=>
in the beginning, in comparison to 2.7.7.final result looks like a regression.scala 2.7.7.final:
found
is seemingly right but code is still not working.I searched on scala bugtracker for similar issues but could not find anything suitable. Think it's enough to create a ticket (or two? looks like this two errors are not related).