具有反向关联中缀表示法的柯里化函数的部分应用语法

发布于 2024-12-09 09:24:12 字数 523 浏览 1 评论 0原文

换句话说,是否有充分的理由不能编译?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝海 2024-12-16 09:24:12

我刚刚修复了这个问题,但我还无法签入它,因为它需要修改规范。

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

问题是,如果 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.

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

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.

顾冷 2024-12-16 09:24:12

看起来像一个编译器错误。我已经在不同的 scala 版本和我得到的结果上测试了这个表达式:

def f(xs: List[Int]) = (0 /: xs) _

它对于 2.9.1.final2.8.2.final 的行为是相同的,但是对于2.7.7.final 它会触发不同的错误消息(IterableTraversableOnes),但我认为这是因为旧版本中集合库的重新设计。

def f(xs: List[Int]) = (0 /: xs) _
<console>:4: error: missing arguments for method /: in trait Iterable;
follow this method with `_' if you want to treat it as a partially applied function

我在评论中提到的表达式对于不同的 scala 版本表现不同。

def f(xs: List[Int]): (Int, Int) => Int => Int = (0 /: xs)

scala 2.9.1.final:

 found   : (Int, Int) => Int => Int
 required: (Int, Int) => Int => Int

确实令人困惑的编译器消息,绝对是一个错误。

scala 2.8.2.final:

 found   : => ((Int, Int) => Int) => Int
 required: (Int, Int) => (Int) => Int

一开始就很奇怪 => ,与 2.7.7.final 结果相比,看起来像是回归。

scala 2.7.7.final:

 found   : ((Int, Int) => Int) => Int
 required: (Int, Int) => (Int) => Int

found 看起来是正确的,但代码仍然无法工作。

我在 scala bugtracker 上搜索了类似的问题,但找不到任何合适的内容。认为创建一个票证就足够了(或两个?看起来这两个错误不相关)。

Looks like a compiler bug. I've tested this expressions on different scala versions and what I've got for:

def f(xs: List[Int]) = (0 /: xs) _

It behaves the same for 2.9.1.final and 2.8.2.final but for 2.7.7.final it fires different error message (Iterable vs. TraversableOnes), but I think it's because of collections library redesign in older versions.

def f(xs: List[Int]) = (0 /: xs) _
<console>:4: error: missing arguments for method /: in trait Iterable;
follow this method with `_' if you want to treat it as a partially applied function

Expression, that I mentioned in comment behaves different for different scala versions.

def f(xs: List[Int]): (Int, Int) => Int => Int = (0 /: xs)

scala 2.9.1.final:

 found   : (Int, Int) => Int => Int
 required: (Int, Int) => Int => Int

Really confusing compiler message, definitely a bug.

scala 2.8.2.final:

 found   : => ((Int, Int) => Int) => Int
 required: (Int, Int) => (Int) => Int

Weird => in the beginning, in comparison to 2.7.7.final result looks like a regression.

scala 2.7.7.final:

 found   : ((Int, Int) => Int) => Int
 required: (Int, Int) => (Int) => Int

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文