On Sat, Jul 12, 2008 at 12:30:02AM -0400, Bob Rogers wrote: > ... But, as I quoted you: > > . . . if cloning works the same as newclosure than we don't need > an explicit newclosure . . . > > Which seems to say something entirely different. So I thought I should > point out that cloning does *not* work the same as newclosure.
Okay, now I understand where you were coming from. I should have said "... in the new scheme, cloning the (already-bound) inner closures will have the same effect that newclosure does now. Since assignment already makes a copy (clone), we won't need an explicit newclosure." > (And I still don't understand the *point* of cloning a closure.) Short answer: cloning is what will enable the following to work: for 1..10 -> $x { sub foo() { say $x; } push(@foos, &foo); } Longer answer: Assume under my proposal that we don't have (or need) a newclosure opcode. Here's how things work: In the above for loop, every time we execute the body of the loop, a capture operation is performed on foo to give it the current variable bindings. We don't get a new object for foo, we just take the existing one and bind it to the new lexical environment (losing any bindings we might have had from a previous capture of foo). In the push statement, however, we want to store a copy of foo as it's currently bound. Thus we simply do a clone of the current foo, which creates a copy (including its current bindings) and stores it in the @foos array. The & in &foo is what cues us to know that we need to a clone, because we're using a closure in an rvalue context. At the start of the next iteration of the for loop, we again perform a capture on the foo sub to bind it to the new lexical environment, However, this doesn't affect the cloned copy that we saved in the previous iteration -- that copy is still bound to the lexical environment of the previous iteration. Which is exactly what we want. Hope this helps. As I mentioned in my reply to chromatic a few minutes ago, I have a *lot* more to add about this, but it will take me a few hours to write it all up clearly. Stay tuned. Pm