Placeholder Syntax not working while uing println


I am learning scala place holder usage for partilly applied functions.
Below is my question. I am creating a list with 3 integer elements and printing it using println

scala> val somenumbers= List(1,2,3)
somenumbers: List[Int] = List(1, 2, 3)

scala> somenumbers.foreach(println _)

when I try printing println _ +1 , scala does not recognize the place holder? PFB

scala> somenumbers.foreach(println _ + 1)
:13: error: type mismatch;
found : Int(1)
required: String
somenumbers.foreach(println _ + 1)
Please help me find out what I am doing wrong…


println is a method. The arguments you pass to a method should be between parentheses, unless you use it as an infix operator. In a better world that compile error would read value + is not a member of () => Unit.
Unfortunately println(_ + 1) won’t work either. You can read more about that here.


Let me add to the answer to emphasize one point:

I think you mean println(_ + 1), but Scalac understands something closer to (println _) + 1.

In general, operator precedence can sometimes be confusing; operator precedence when creating functions (without or with underscore) creates type errors that look essentially random at first sight.

To be able to debug such issues on your own, try adding parentheses where expected to see if they make a difference (even just a different error).
Since it’s sometimes not obvious how placeholder syntax is expanded, you can try expanding it by hand and seeing if it makes a difference.