On Tue, Aug 01, 2006 at 09:03:50PM +0900, dblack / wobblini.net wrote:
>
> No; what makes something a closure, as I understand it, is that it
> carries the context of its creation with it. Also, there's actually a
> difference between a block on the one hand, and a Proc or lambda on
> the other. (See my last couple of posts.)
That's far too broad to even be a meaningful distinction. It's
tantamount to claiming that in a program where everything has lexical
scope, everything is a closure -- because everything's "context"
(essentially its scope) is always applicable to it as it was at the
point of its creation with lexical scope.
>
> I think of a closure as a kind of suitcase: you pack it in one place,
> and unpack it somewhere else. Even if it's empty, though, it's still
> a suitcase.
That makes more sense than what you said above (that I quoted above and
to which I responded above): it sounds like we're back to the koan here.
Specifically, it sounds like what you're saying here would indicate that
this is a closure:
def foo
bar = 0
lambda { puts bar += 1 }
end
baz = foo
and this is a closure:
def foo
lambda { puts "Hellow world!" }
end
bar = foo
because it does the same things with scope, but this is not a closure:
foo = 0
foo.each do
puts bar += 1
end
because its functionally equivalent to accessing global scope (which
even GWBASIC can do). Unfortunately, I suspect that you would say that
last example actually is a closure, and your "think of a closure"
description above was simply not precise enough.
I'd accept the description you gave above as being koan-compliant, with
the first two being closures within a broad but theoretically applicable
definition of a closure, as long as the apparent meaning of your
definition-by-analogy that excludes the third example is the definition
you're using. Once you interpret it to include the third definition, I
have a distinct problem with it.
>
> Another way to look at it is this: if you decide that the lambda in
> your example above is not a closure, then you have to come up with
> separate explanations for everything it does that is closure-like.
> If you look at it as a closure, however, there's nothing unaccounted
> for.
. . . and that's why it makes sense to think of it as fitting a
koan-compliant definition of a closure as you explained above, so long
as what it carries around with it is actually *a lexically closed
scope*, even if there's nothing meaningful within that closed scope.
That still doesn't make all blocks closures, though.
--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
print substr("Just another Perl hacker", 0, -2);