user972946
user972946

Reputation:

Why does this type error occur?

See the following function definition with implicit return type:

scala> def getIn(nodes:NodeSeq, path:String) = {
     | nodes map {node => node \ path}
     | }
getIn: (nodes: scala.xml.NodeSeq, path: String)scala.collection.immutable.Seq[scala.xml.NodeSeq]

However, if the function is defined with the same return type explicitly, an error occurs:

scala> def getIn(nodes:NodeSeq, path:String) = Seq[NodeSeq] {
     | nodes map {node => node \ path}
     | }
<console>:9: error: type mismatch;
 found   : scala.collection.immutable.Seq[scala.xml.NodeSeq]
 required: scala.xml.NodeSeq
       nodes map {node => node \ path}

I cannot wrap my head around why the error occurs. Please help me.

Upvotes: 0

Views: 90

Answers (1)

Brian Agnew
Brian Agnew

Reputation: 272207

In your second case, you're not defining the return type correctly. You need

def getIn(nodes:NodeSeq, path:String) : Seq[NodeSeq] = { ...

Upvotes: 4

Related Questions