tags:

views:

92

answers:

2

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

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:_*)
  }

The compiler error is

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

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?

+3  A: 

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:_*)
}

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.

Ben Lings
+2  A: 

If you don't use operator syntax, it compiles fine:

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

There is not a problem with how you use the anonymous function, but as Ben mentioned, the syntax of calls map without the . is not equivalent to the typical Java-style method call.

Justin Ardini