I tried to make an example to explain that the placeholder _ without parentheses can represent (be expanded to) any number of parameters with any type, not just can represent "only one" parameter with any type. However, the one I made was incorrect since the parameter (function literal) of foreach would still take one parameter only.
// The following code to explain the placeholder rule above is incorrect.
I made a modified example to expound this rule simply.

val list1 = List(1,2,3)
val list2 = List((1,2),(3,4),(5,6))
val list3 = List((1,2,3),(4,5,6),(7,8,9))
scala> list1.foreach(println _) // _ is expanded to 1 parameter in each iteration
1
2
3

scala> list2.foreach(println _) // _ is expanded to 2 parameters in each iteration
(1,2)
(3,4)
(5,6)

scala> list3.foreach(println _) // _ is expanded to 3 parameters in each iteration
(1,2,3)
(4,5,6)
(7,8,9)

This might be able to explain the rule more clearly.
I hope it is correct.

// The original question
In Chapter 8.6 PARTIALLY APPLIED FUNCTIONS of the book Programming in Scala, 3rd Edition, An example shows:

val list = List(1,2,3)
list.foreach(x => println(x))

The context says the function literal

println _

can substitute for

x => println(x)

because the _ can represent an entire parameter list.

I know an underscore leaving a space between itself and the function name (println, in this case) means the underscore represents an entire parameter list.
In this case, however, there is only one parameter (the Int element of each iteration) in the original function literal.
Why does this tutorial say _ represents an entire parameter list?

The function literal

x => println(x) // Only one parameter? Where's the entire parameter list? 

in

list.foreach(x => println(x))

obviously has only one parameter, correct?

1

There are 1 best solutions below

3
On BEST ANSWER

Why does this tutorial say _ represents an entire parameter list?

Because it's talking about the entire parameter list of println. Which has only one parameter.

Do you mean println _ represents println(element1: Int, element2: Int, ... elementN: Int)

No. To determine the meaning of println _ we look at its signature

def println(x: Any): Unit

"The entire parameter list" is (x: Any), so println _ is the same as (x: Any) => println(x). If you have def foo(x: Int, y: Int) = x + y, then foo _ will be (x: Int, y: Int) => foo(x, y).

Note: There is also the overload with no parameters def println(): Unit, but the compiler determines it makes no sense here because foreach expects a function with a single parameter; but e.g. in

val f: () => Unit = println _

println _ is equivalent to () => println() instead of (x: Any) => println(x).