Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 使用反向关联中缀符号部分应用curried函数的语法_Scala_Currying_Infix Notation_Partial Application_Associativity - Fatal编程技术网

Scala 使用反向关联中缀符号部分应用curried函数的语法

Scala 使用反向关联中缀符号部分应用curried函数的语法,scala,currying,infix-notation,partial-application,associativity,Scala,Currying,Infix Notation,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 m

换句话说,有没有一个很好的理由不应该编译它

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

但我的问题主要是关于正确的语法。

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

def f(xs: List[Int]) = (0 /: xs) _
它对
2.9.1.final
2.8.2.final
的行为相同,但对于
2.7.7.final
它会触发不同的错误消息(
Iterable
TraversableOne
),但我认为这是因为旧版本中的馆藏库重新设计

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
def f(xs: List[Int]): (Int, Int) => Int => Int = (0 /: xs)
scala 2.9.1.1最终版本:

 found   : (Int, Int) => Int => Int
 required: (Int, Int) => Int => Int
真让人困惑的编译器消息,肯定是个bug

scala 2.8.2.1最终版本:

 found   : => ((Int, Int) => Int) => Int
 required: (Int, Int) => (Int) => Int
奇怪的
=>
在开始时,与2.7.7相比。最终结果看起来像是回归

scala 2.7.7.1最终版本:

 found   : ((Int, Int) => Int) => Int
 required: (Int, Int) => (Int) => Int
found
似乎是正确的,但代码仍然不起作用


我继续寻找类似的问题,但找不到任何合适的。我认为创建一个票证就足够了(或者两个?看起来这两个错误没有关系)。

我刚刚修复了这个问题,但是我还不能签入它,因为它需要修改规范

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
scala>def(xs:List[Int])=(0/:xs)_
f:(xs:List[Int])(Int,Int)=>Int=>Int
scala>f(1到10个toList)
res0:(Int,Int)=>Int=>Int=
scala>res0(u+u)
res1:Int=55

问题是,如果op与{val x=e1;e2.op(x)}是右关联的,规范定义了“e1 op e2”,原因我并不清楚,因为更简单的e2.op(e1)解决了这个问题,比如。我将进行查询。

+1,我刚刚发现了另一个令人困惑的编译器错误,在表达式
def(xs:List[Int])中省略了一对括号:(Int,Int)=>Int=>Int=(xs:\0)
found:(Int,Int)=>Int=>Int
Int=>Int对我来说,第一次打开似乎是一个bug,除非有人能说不同的话;第二个可能与否有关,不要屏住呼吸。影响太大了。
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