Block Closure & fixTemps (was Squeak textbox and events)

Scott A Crosby crosby at qwes.math.cmu.edu
Sun May 13 20:13:55 UTC 2001


On Sun, 13 May 2001, Tim Rowledge wrote:

> "Noel J. Bergman" <noel at devtech.com> is widely believed to have written:
>
> > Bert,
> >
> > So to "fix" Scott's example, this:
> >
> >  x := [ :foo |
> >          |temp|
> >          temp := foo.
> >          [ temp ] ].
> >
> >  y := x value: 12.
> >  z := x value: 13.
> >

Completing the example:

``
What happens with
  y value.
  z value.

Do you get 12&13? Or 13&13?
''

> > would become this:
> >
> >  x := [ :foo |
> >          |temp|
> >          temp := foo.
> >          [ temp ] fixtemps].
> nope, more like
> 	x _ [:foo|
> 		|temp|
> 		temp _ foo] fixTemps
>

This doesn't appear to be a closure; IE, I'm not returning a block that's
closed over a variable in an outer environment. The first example with
'[temp]' is a closure; its closed over the binding of 'temp'.

Maybe you're slightly confused.. The example above omitted two
lines. (See above)


BTW, if fixTemps copies over all bindings that are closed over, well,
that's not quite closure semantics either:


block :=
[
  | bag foo bar |
  bag := Array new: 2.
  bag at: 0 put: [ x | Transcript show: bar.
                   foo := x] fixtemps.

  bag at: 1 put: [ y | Transcript show: foo.
                   bar := y] fixtemps.
  bag.
]

bag := block value.
fst := bag at: 0.
snd := bag at: 1.

fst value: 10  "Should print nil"
snd value: 20  "Should print 10, but if the two blocks get seperate
                instances of the temporary variables foo,bar, they can't
                mutate each other, and it'll print 'nil"
fst value: 30  "Should print 20, but again will print nil?"

--

A closure closes over a single binding of a variable, so it must be
shared with all other code that experiences the same binding of the
variable, and distinct from all other code that gets a seperate binding.
 * guessing from the description, fixTemps doesn't share where it should?
 * Blockcontexts, as we know, currently share everything.

But, THANKS for the fixTemps info. it's better than everything being
shared.

True, the smalltalk idiom might, in this case, define the two blocks to be
seperate methods in a class, and have the class instance hold the 'foo'
and 'bar' variables. then return an array of selector-names which are then
perform:'ed.

Yes, I'm a programming language weenie who likes his functional langauges.
Given what I've seen here, I should probably edit up what I have
explaining closures and add it to the Swiki.

So can you elaborate if Noel had the right idea or not about fixTemps?


Scott





More information about the Squeak-dev mailing list