I'm a total Scala newbie and at a loss as to what is going on here:
(1 to 2) foreach {
println("+")
v => { println(v) }
println("-")
}
Why does this print the following?
+
1
-
2
-
I think it has something to do with an instance being created implicitly and the body being executed as the constructor, which would explain the +
being printed once. But that doesn't explain why the -
is printed after each loop.
I'm sorry if this is a silly question. This just keeps nagging me. Debug execution inside Eclipse didn't reveal anything enlightening either.
And to add to the question: Why is this valid? I've understood that foreach
should accept one parameter, a function, which receives an Int
and returns, umm.. something.