I got the book a couple of hours ago and I am thoroughly enjoying it. However one sentence in the book caught my eye because it seems a bit rough and unedited.
Page 148,
"But because all anonynmous functions are turned into closures at runtime, we often simply call them closures. "
I hope I am not seeing things but shouldn't there be a distinction between anonymous functions (a source level concept) and closures? (an implementation level concept).
After all named functions also "close over" variables don't they?
I am confused because the paragraph just above the quoted sentence attempts to distinguish anonymous functions and closures and then this sentence equates them again!
The justification seems dicey too. Why should an implementation technique leak into what a language feature is named? If all methods of a class are turned into entries in a m_table (just creating a term here for illustration) at runtime we don't call methods "m_table entries" do we?
I would assume that named functions also close over variables in the Scala implementation (I have NOT looked at the source code, but I can't imagine that this doesn't happen) so I don't quite understand equating anonymous functions and closures!
I could be totally wrong. If so please ignore this, but it *seems* to me that there is some confusion here and I would prefer that *in Scala* we use the most precise terminology we can.
Imho, the book will definitely be better for distinguishing these concepts. There is enough fuzzy thinking in the world of programming languages (and books on these languages) without this book (which is a very pleasant read so far btw) adding to it.
This issue was also brought up in the Scala mailing list. We're going to try using function literal and function value in the next PrePrint version, and also spend more time explaining what closure really means. To clarify that one sentence, basically what we were saying is that people will commonly point to an anonymous function in code and say "this closure is passed into the method here." So they seem to be talking about anonymous functions as if they are closures, but what I think they are really doing is animating their code when they talk about it. They are talking about what happens at runtime, and what happens at runtime is that a closure is instantiated from the anonymous function and passed into the method. That last sentence from the glossary that you quoted didn't explain that very well, but that was the intent.
What we would like to do is very clearly and formally define terms as we use them, then use them consistently that way throughout the book. The reason is that sloppy use of terms form an unnecessary barrier to learning. So we appreciate this kind of feedback.