Re: I thought this was the one that worked?



On Tue, Aug 01, 2006 at 11:39:29AM +0900, Hal Fulton wrote:
Chad Perrin wrote:

Am I to assume that some_local_variable is going out of scope outside of
the lambda declaration at some point, to create that closure?



All things that are called "blocks" in Ruby are closures.
Do you still have any questions on the matter?


Yes. Are you just failing to explain how these things are closures, or
do you not know what a closure is? You keep telling me blocks are
closures somehow, but you're not explaining how. Saying it doesn't make
it so. Please explain what I'm missing.


I'm unfamiliar with your assertion that the context has to go
out of scope before a closure exists.

Not trying to argue, just stating my perception.

Maybe you can point us to a formal definition somewhere.

How's this?:

Closure
A "closure" is an expression (typically a function) that can have
free variables together with an environment that binds those
variables (that "closes" the expression).

I found it on this page:

http://jibbering.com/faq/faq_notes/closures.html

I also found this:

http://www.perl.com/pub/a/2002/05/29/closure.html



I would have said that a closure *will* retain its variables
*if/when* they go out of scope.

Bad analogy: We might loosely define a car as "a machine that
goes places," but it's still a car even when it sits still.

My understanding is that the vehicle is a car, and the use of it is a
drive (to the country, or whatever). Similarly, a block of code that
can be passed around like a first-class member is a block, or lambda, or
whatever you want to call it today: if you pass it to a function as an
argument, it's a callback, and if you lexically close it, it's a
closure.



My impression is that you're applying the definition too
rigidly.

I suppose that's possible, but I really don't think so. Do you have a
formal definition of a closure that specifically contradicts my
understanding?

--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
"A script is what you give the actors. A program
is what you give the audience." - Larry Wall

.



Relevant Pages

  • Re: Function question - final part (closure)
    ... if I now take 'foo' in function bar and replace it with foo's ... scope chain at that point is stored as part of it, ... creates a closure containing the scope at that point. ... which the global scope didn't. ...
    (comp.lang.javascript)
  • Re: I thought this was the one that worked?
    ... the term "closure" in computer science is derived from the ... science refers to 'enclosing' the present scope. ... This subroutine is a closure because it refers to the lexical $count ... Closures are "closed" only on lexical variables, ...
    (comp.lang.ruby)
  • Re: this confusion
    ... It is the sufficient criterion for a closure. ... I was under impression that returned function object forms a closure during its evaluation as a function expression; that this closure includes calling context scope - context of an outer self-executing function - and that this scope has `x` as a property of its Activation/Variable object and so can be resolved from within returned `fn`: ...
    (comp.lang.javascript)
  • Re: I thought this was the one that worked?
    ... the requirements of a closure, though, at this point. ... What current scope are you referring to here? ... In this sense, Ruby ... and thus necessitates using terms in a ...
    (comp.lang.ruby)
  • Re: this confusion
    ... which would be on the scope chain anyway. ... in the source code of the function? ... It is the sufficient criterion for a closure. ... Identifier resolution *along the scope chain* takes place when the function ...
    (comp.lang.javascript)