views:

116

answers:

2
List("This","is","Scala").foreach(a => print(a+" "))

compiles fine, but

List("This","is","Scala").foreach(print(_+" "))

fails complaining of missing parameter type. I couldn't figure out why it fails.

EDIT: I meant print not println - not that it makes logical difference.

+6  A: 

Scala reads (_+" ") as x => x+" ". But println doesn't contain any type information to help the compiler guess what type x might be, so it gives that error.

What you wanted it to do was notice that println didn't work and recurse backwards and try it again for foreach. But it can't do that: println can take any argument, including a function, so x => x+" " is a perfectly valid thing for println to try to print.

(And even in a case where it could in theory backtrack, it generally doesn't.)

Rex Kerr
+5  A: 

The problem is that this

List("This","is","Scala").foreach(print(_+" "))

is not equivalent to

List("This","is","Scala").foreach(a => print(a+" "))

but to

List("This","is","Scala").foreach(print(a => a+" "))

Now, let's see the type signature of foreach:

def foreach [B] (f: (A) ⇒ B) : Unit

where A is the type parameter of the List itself. Since we have a List[String], the compiler knows one has to pass to foreach a Function[String, B].

In a => print(a+" ") the type of a is already known then: String.

In print(a => a+" ") there is a problem, as print is not a Function. However, the compiler hasn't considered that yet -- it's still trying to compile a => a+" ". So let's look at the type of Predef.print:

def print (x: Any) : Unit

So a => a+" " must be of type Any, which, of course, means it can be anything. It doesn't help the compiler in asserting what the type of a is. Which doesn't really matter, because you didn't want to print a Function in first place.

Daniel