I said there has to be a =>. It doesn't have to be explicit in the code.
Besides for-comprehensions, another way there can be a => without you
writing it at the time is a by-name parameter..
def run10Times(x: => Unit) = 1 to 10 foreach (y => x)
var count = 0
run10Times {
count += 1
println(count)
} // look, ma, no explicit =>, but as it's in the parameter declaration it
still forms a closure and hence a new class file.
I can't comment on the 'I'm likely wrong here' part as I couldn't follow
you there.
Your rule of thumb is a good one for current Scala and Java.
On Fri, Jul 27, 2012 at 12:17 PM, Josh Berry <[email protected]> wrote:
> On Fri, Jul 27, 2012 at 10:16 AM, Ricky Clarkson
> <[email protected]> wrote:
> > Like I said, without a => there's no closure.
> >
> > The for comprehension works because it gets converted into code
> containing a
> > =>.
>
> These two sentences don't make sense together. Either you have to
> provide the => or you don't. That one will be implied for you goes
> against what you are saying. In fact, I think the confusing thing
> here is that the "closure" in the first section is a function from
> unit to whatever the closure returns. It seems the compiler works
> with you by converting that to what the closure returns through
> evaluation. (I am more than willing to admit I'm likely wrong here.)
>
>
> > A block in a position where a function is expected is just a block whose
> > value needs to be a function. Does that make sense now? I can probably
> > find some specspeak that explains it better if not.
>
> This doesn't make sense because this only worked since you have it
> return a function that can work there. Specifically, the unapplied
> println. That is, this didn't work because it was a block where a
> function is needed, but because it was a block "returning" a function
> that was needed. Something a block doesn't do. (return, that is.)
>
>
> The simple rule of thumb I have is if a new class file gets created to
> support a block, then it was a closure. :) Not always obvious from
> just looking at the source. Using this guide, you'll find that
> changing from:
>
> object Test {
> def main(args:Array[String]) {
> println("Hello")
> }
> }
>
> to
>
>
> object Test {
> def main(args:Array[String]) {
> 1 to 10 map println
> }
> }
>
> Results in a new classfile generated. For the anonfun that is the
> unapplied println.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Java Posse" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/javaposse?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups "Java
Posse" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/javaposse?hl=en.