[Prev][Next][Index][Thread]
Re: Closures
On Saturday, December 1, 2001, at 01:45 AM, Doug Hockin wrote:
> Somehow this seems to violate Dylan's separation of
> objects and functions. The closure created function
> seems to have associated with it an object with slot
> points. What am I missing?
A closure can be thought of as a function *with* an associated
environment. That environment "remembers" the bindings of all the
relevant symbols for that function. In the DRM example, the method
make-score returns a method which increases points by the amount
increase which is passed to it. In order for this returned method to
work properly, it must "remember" the binding (value) of points from one
invocation to the next, which is the behavior you observe. If it didn't
"remember" the binding of points, the score would be reset to it's
initial value with each invocation, which is not what we want (I can't
help thinking that a bank account deposit/withdrawal method might have
been more illustrative here - after all, people take for granted that
resetting your bank account to your opening balance with each
transaction is the *wrong* thing to do).
I think you can see that it is often useful to be able to return a
method complete with the bindings relevant to that method's proper
operation, rather than returning a method with amnesia, which you would
then have to call with all the relevant updated objects at each
invocation. That's what closures are for (among many other creative
uses, such as that alluded to by Gabor).
Hope this helps,
Raf
References:
- Closures
- From: Doug Hockin <dhockin@staffware-spokane.com>