2

I have already read some other questions like What are all the uses of an underscore in Scala? and while I'm sure this question has already been asked, I can't go through all of the other 17000 Scala questions.

There is Foreach have strange behaviour and Placeholder not useful but it still seems to be a hidden feature:

scala> val is = (1 to 5) toList
is: List[Int] = List(1, 2, 3, 4, 5)

scala> is foreach { i => println("Hi.") ; Console println 2 * i }
Hi.
2
Hi.
4
Hi.
6
Hi.
8
Hi.
10

scala> is foreach { println("Hi.") ; Console println 2 * _ }
Hi.
2
4
6
8
10

Can someone explain me please the difference?

If you feel a burst of enthusiasm and try:

scala> is foreach { i => println("Hi!") ; Console println 2 * i }
java.lang.IllegalArgumentException: !") ; Console println 2 * i }: event not found

then see this answer. And yes that really happened.

3
  • Editor's note: This question was a lot funnier before @sschaef's intervention. It's incumbent upon the critics to locate the dupe if it exists; several pages of google results say no.
    – som-snytt
    Jul 15, 2013 at 19:48
  • I don't see what was so funny, I'm sorry when I have destroyed your gags. For me it looked like the intentions of a drunken master. And there are some dups, at least I'm too lazy to search them.
    – kiritsuku
    Jul 15, 2013 at 20:28
  • @sschaef Funny to me, that is. There are N ways of doing everything in Scala (they say) resulting in exponential questions. But thanks for cleaning it up.
    – som-snytt
    Jul 15, 2013 at 20:40

2 Answers 2

7

Placeholder syntax works on one expression, not for the whole block, so your example is interpreted as { println("Hi."); i => Console println 2 * i }

1
3

Landei has the right answer, I think, but it's not unpacked very thoroughly. Let's start at the end:

scala> Console println 2 * _
<console>:8: error: missing parameter type for expanded function
((x$1) => Console.println(2.$times(x$1)))
              Console println 2 * _
                                  ^

Okay, so we see that on its own, Console println 2 * _ is trying to create a function via explicit eta expansion, except it doesn't know the return parameter type so it can't.

Now let's try a code block that returns a function.

scala> { println("Hi"); (i: Int) => i*5 }
Hi
res1: Int => Int = <function1>

So, as with everything, you execute the entire block (including side-effecting statements like println), and return the return value which is your function.

Now, as Landei said, placeholder syntax only works for one argument in one (simple) expression, and in the second case we don't have a simple expression but a block expression (consisting of two simple expressions). So we're not using placeholder syntax, we're creating a function. And we do it in a code block:

is foreach { println("Hi.") ; Console println 2 * _ }

which, since we don't start with function arguments, is interpreted as a plain parameter, except we can in almost any context--argument lists included--replace a simple expression (x) with a block expression { stuff; x }. So we can think of it as

is foreach ({ println("Hi.") ; Console.println 2 * _ })

Now the type inferencer knows what the return type is supposed to be, so it runs that code block which prints out "Hi" and creates a function, then passes that function (just once, at the beginning!) to foreach. If the type inferencer could look across lines to figure out types, it would be equivalent to this:

val temp = { println("Hi."); Console.println 2 * _ }
is foreach (temp)
2
  • "since we don't have just an expression" is misleading. We have block expression, and an arg list can be either (exprs) or {block}, there's no sense (implicitly, conceptually) in which it must be wrapped in parens. A block expr includes both { block } and { cases }.
    – som-snytt
    Jul 15, 2013 at 20:22
  • @som-snytt - I've improved my description with more technically correct terminology.
    – Rex Kerr
    Jul 15, 2013 at 20:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.