From: ara.t.howard@... Date: 2006-08-02T08:05:02+09:00 Subject: Re: I thought this was the one that worked? On Wed, 2 Aug 2006, Chad Perrin wrote: > No, it refers to "closing" the scope, not "enclosing" the scope. To enclose > the scope, you have to be outside of it. To close the scope, you seal it up > so that stuff doesn't wander in and out of it. and yet harp:~ > ruby -e' closure = lambda{}; def a() 42 end; eval "p a", closure; ' 42 harp:~ > ruby -e' def a() 42 end; closure = lambda{}; eval "undef a", closure; a() ' -e:1: undefined method `a' for main:Object (NoMethodError) stuff wanders in, stuff wanders out. same goes for perl, lisp, etc, etc. they, like ruby, have 'real' closures. closures are not closed in that (dynamic) sense. what the 'closed' in closure means is that you cannot get 'out' of it. in otherwords harp:~ > ruby -e' closure = lambda{}; eval "a, b = 40, 2; c = a + b; p c", closure; p c ' 42 -e:1: undefined local variable or method `c' for main:Object (NameError) in mathmatical terms operations on members of the set produces more members of that set (meaning in that scope here). > I don't assume that to have inflammatory intent. I only assume you're > confused about the difference between "close" and "enclose". well - i think i understand exactly how closures work to enclose a given scope in ruby while 'closing' nothing but lexical definitions - so we'll have to agree to disagree here. > Read Intermediate Perl, then come back and say that again. > > I will quote from its original version (which actually had a different > title, but it's still the first edition of Intermediate Perl) for you: > > The kind of subroutine that can access all lexical variables that > existed at the time it was declared is called a _closure_ (a term > borrowed from the world of mathematics). the term, not the exact meaning. topo maps have closures too. > This subroutine is a closure because it refers to the lexical $count > variable. > > At the end of the naked block, the $count variable goes out of scope. > However, because it is still referenced by the subroutine in > $callback, it stays alive, now as an anonymous scalar variable. > > Here's another quote from the same book: > > Closures are "closed" only on lexical variables, since lexical > variables eventually go out of scope. Because a package variable > (which is a global) never goes out of scope, a closure never closes on > a package variable. All subroutines refer to the same single instance > of the global variable. > > (meaning: global to the "package", aka "namespace") indeed. and this is like ruby, which is also closed on lexical variables __only__. note that this is not closed harp:~ > cat a.pl $s = sub{ a(); }; sub a { print "42\n"; }; &$s; harp:~ > perl a.pl 42 the created closure encloses the scope at the time of creation - including both lexical and dynamic scopes - to which the subroutine 'a' is added. the lexical scope is indeed closed though. > Here's yet another: > > A subroutine doesn't have to be an anonymous subroutine to be a > closure. If a named subroutine accesses lexical variables and those > variables go out of scope, the named subroutine retains a reference to > the lexicals, just as you saw with anonymous subroutines. > > There are four distinct sections of Chapter 6: Subroutine References > that deal specifically with closures -- thus the wealth of quotes on the > subject. not very much info on dynamic scoping though - the main focus is on lexical scoping. > In case you're wondering about the credentials of the author of this > book, his name is Randal L. Schwartz. You may have heard of the > "Schwartzian transform". i've read the book many times - i was perl hacker before coming to ruby. -a -- we can never obtain peace in the outer world until we make peace with ourselves. - h.h. the 14th dali lama