为什么这些语句中的一个在 Scala 中编译而不是另一个? [英] Why does one of these statements compile in Scala but not the other?

查看:20
本文介绍了为什么这些语句中的一个在 Scala 中编译而不是另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(注意:我在这里使用的是 Scala 2.7.7,而不是 2.8).

(Note: I'm using Scala 2.7.7 here, not 2.8).

我正在做一些非常简单的事情——基于一个简单的 2 列 CSV 文件中的值创建一个地图——我已经很容易地完成了它,但我很困惑为什么我的第一次尝试没有不编译.代码如下:

I'm doing something pretty simple -- creating a map based on the values in a simple, 2-column CSV file -- and I've completed it easily enough, but I'm perplexed at why my first attempt didn't compile. Here's the code:

// Returns Iterator[String]
private def getLines = Source.fromFile(csvFilePath).getLines

// This doesn't compile:
def mapping: Map[String,String] = {
    Map(getLines map { line: String =>
          val pairArr = line.split(",")
          pairArr(0) -> pairArr(1).trim()
        }.toList:_*)
  }

// This DOES compile
def mapping: Map[String,String] = {
    def strPair(line: String): (String,String) = {
      val pairArr = line.split(",")
      pairArr(0) -> pairArr(1).trim()
    }
    Map(getLines.map( strPair(_) ).toList:_*)
  }

编译错误是

CsvReader.scala:16:错误:值 toList 不是(字符串) => (java.lang.String,java.lang.String) [scalac] 可能原因:可能缺少分号在value toList"之前?[scalac]
}.toList:_*) [scalac] ^
[scalac] 发现一处错误

CsvReader.scala:16: error: value toList is not a member of (St ring) => (java.lang.String, java.lang.String) [scalac] possible cause: maybe a semicolon is missing before `value toList'? [scalac]
}.toList:_*) [scalac] ^
[scalac] one error found

那是什么?除了显式函数定义(与非工作示例中的匿名)和 () 与 {} 之外,它们似乎应该与我等效.如果我在非工作示例中用括号替换花括号,则错误为';'意料之中,但找到了 'val'."但是如果我删除局部变量定义并将字符串拆分两次并使用括号而不是花括号,它就会编译.有人可以向我解释这种区别吗,最好附上 Scala 文档的链接来解释括号和花括号在用于包围方法参数时的区别?

So what gives? They seem like they should be equivalent to me, apart from the explicit function definition (vs. anonymous in the nonworking example) and () vs. {}. If I replace the curly braces with parentheses in the nonworking example, the error is "';' expected, but 'val' found." But if I remove the local variable definition and split the string twice AND use parens instead of curly braces, it compiles. Can someone explain this difference to me, preferably with a link to Scala docs explaining the difference between parens and curly braces when used to surround method arguments?

推荐答案

看起来不同是因为您在第一个示例中使用了运算符表示法.如果您添加一组额外的括号,它会起作用:

Looks like the difference is because you are using the operator notation in the first example. If you add an extra set of parentheses it works:

def mapping: Map[String,String] = {
    Map((getLines map { line: String =>
      val pairArr = line.split(",")
      pairArr(0) -> pairArr(1).trim()
    }).toList:_*)
}

或者如果你不使用它的操作符语法

or if you don't use the operator syntax it works

def mapping: Map[String,String] = {
    Map(getLines.map({ line: String =>
      val pairArr = line.split(",")
      pairArr(0) -> pairArr(1).trim()
    }).toList:_*)
}

我认为问题是使用普通方法调用语法比方法调用的操作符语法具有更高的优先级.这意味着 .toList 被应用于匿名函数而不是 map 方法调用的结果.

I think the problem is that using the normal method invocation syntax has higher precedence than the operator syntax for method calls. This meant that the .toList was being applied to the anonymous function rather than to the result of the map method call.

这篇关于为什么这些语句中的一个在 Scala 中编译而不是另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆