[self-interest] blocks and closures
tb at becket.net
tb at becket.net
Thu Apr 10 05:31:46 UTC 2003
James McCartney <asynth at io.com> writes:
> It depends on the Smalltalk.
> A full closure can be returned from a function and still refer to that
> function's variables. Self disallows this, as do some Smalltalks I
> beleive. If you support this feature you can no longer put all your
> local variables on the stack, because references to them may outlive
> the function activation in which they were created.
This is a fiction that the Smalltalk crowd likes to talk about, and
it's a shame that the Self crowd also repeats it.
In languages which support closures, it is rare to return them. Most
functions never return them. So you can store such local variables on
the stack just fine, for all but the minority of functions which
But what about the ones that do? Well, if you didn't return a
closure, you'd have to do something else to hold the same data, some
kind of allocation, and that has just whatever cost the alternative
non-stack compilation has for the same data.
There is a wrinkle because Smalltalk and Self use internal closures
(whether real or pseudo) for control structures to a higher degree
than Lisp/Scheme usually do. However, it is still very rare to
actually return them.
A Lisp/Scheme system therefore can use non-stack allocation for any
function where a closure is created, even if it is not returned (that
is, if it can be taken to have dynamic extent). But this is not
strictly necessary--Smalltalk and Self do just fine with
pseudo-closures, that is, closures with merely dynamic extent.
So, since Smalltalk/Self code is more aggressive in creating closures,
which are typically not returned, the trick is for the compiler to
detect ones which won't be returned. This is in general a matter of
detecting aliasing, and it's not too tough for the common uses of
closures in Smalltalk and Self.
So, my suggestion is to bite the bullet and do it right. Demand real
If you think this is "too hard", then what are you doing with
Smalltalk and Self anyhow? The whole glory here is that things that
are "too hard" can, indeed, be made quite tractible through good
compilation. In the case of closures, the techniques are well-known,
and it's a shame that the Smalltalk/Self crowd mostly disregard them.
More information about the Self-interest