Block Closure & fixTemps (was Squeak textbox and events)

Bob Arning arning at charm.net
Sun May 13 23:50:11 UTC 2001


Noel,

The out of bounds error results from using zero as an index --- Smalltalk arrays start at 1. If you want foo and bar to be shared by the two inner blocks, remove the #fixTemps. The revised example below does what I think you want: 
-------------------------------------------
result := [
	| foo bar |
  
	{
		[ :x | Transcript show: bar. foo := x].
		[ :y | Transcript show: foo. bar := y]
	}
] fixTemps value.
(result at: 1) value: 10.
(result at: 2) value: 20.
(result at: 1) value: 30.
-------------------------------------------

Cheers,
Bob

On Sun, 13 May 2001 18:18:25 -0400 "Noel J. Bergman" <noel at devtech.com> wrote:
>Yes, with my the fixTemps inserted, I got 12 and 13 as desired.  HOWEVER ...
>
>I have tried your new example:
>
>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?"
>
>As soon as I enter "bag := block value.<doit>", I receive an array out of
>bounds error dealing, I believe, with the first Array>>at:put: invocation.
>Therefore I can't run the rest of the test.





More information about the Squeak-dev mailing list