Block closures was: Block temps was: Porting from other

Lex Spoon lex at cc.gatech.edu
Wed Aug 19 01:08:59 UTC 1998


Christopher Oliver writes:
 > > (Re-entrant) blocks that have their local copies of arguments, stack (and
 > > temps) per activation. You need to separate the block code-representing
 > > objects (where there would be, for example, one per literal block) from the
 > > block-activation representing objects (where there would be one per #value*
 > > message).
 > 
 > Actually, I think it should be a bit trickier still.  Shouldn't a closure
 > capture its environment.  Consider:
 > 
 > 	|a b c|
 > 	a _ [ :j | [ :i | i + j ]].
 > 	b _ a value: 4.
 > 	c _ a value: 10.
 > 	b value: 5
 > 
 > For closures, shouldn't the free variable of the inner block be bound
 > to the value of J at call time.  I know my mind is polluted with Lisp
 > and ML, but it seems as though code/environment pairs are a touch more
 > complex than local copies.
 > 


I think that's actually the idea behind "local copies".  What is an
"environment", but a list of variable assignments?


When you do "a value: 4", a new context is created to hold values of
:j, and the :j in that context is initialized to 4.  That block
returns a block referencing this context, so the context isn't garbage
collected immediately.


The "a value: 10" creates another new context, separate from the
original.  Anything that happens in the new context doesn't effect
what happens in the first context.


When you get down to the "b value: 5", the context that b is using
should still have a 4 in it, and the answer will be 4+5=9.

 Squeak, however, doesn't create new contexts--it just reuses old
ones.  So the 4 gets clobbered by the "a value: 10".  So the answer
will be 10+5=15.


Lex





More information about the Squeak-dev mailing list