Blocks and it's temp vars

Mathieu Suen mathk.sue at gmail.com
Sat Dec 8 23:41:20 UTC 2007


Hi,

I don't think that #fixTemps should fix the problem:

The to:do: block is create once but #fixTemps copy the home context
This copy change the semantic scope of i.

=> Even with full closure your code would not work without #fixTemps.

An other solution is to use curification:

	stuff := #(foo bar).
	results := OrderedCollection new.
	1 to: 2 do:[:i|
		results add: ([:show | [Transcript cr; nextPutAll: show;flush]]  
value: (stuff at: i))
	].
	results do:[:e| e value].

This work with full closure but don't work with squeak block. So you  
still need a #fixTemps if you are not using full closure:

	stuff := #(foo bar).
	results := OrderedCollection new.
	1 to: 2 do:[:i|
		results add: ([:show | [Transcript cr; nextPutAll: show;flush]]  
fixTemps value: (stuff at: i))
	].
	results do:[:e| e value].

Cheers

On Dec 8, 2007, at 8:23 PM, Randal L. Schwartz wrote:

>
> [copied from Seaside list, where this was asked and answered as  
> follows]
>
>>>>>> "Sebastian" == Sebastian Sastre <ssastre at seaswork.com> writes:
>
> Sebastian> 	At first I was suspecting about the #fixCallbackTemps  
> but the block
> Sebastian> was already malformed when reach that point.  
> Investigating about this I'm
> Sebastian> surprised to found that it's related to Squeak being  
> unable to do properly
> Sebastian> this smalltalk code:
>
> Sebastian> 	stuff := #(foo bar).
> Sebastian> 	results := OrderedCollection new.
> Sebastian> 	1 to: 2 do:[:i|
> Sebastian> 		results add: [Transcript cr; nextPutAll: (stuff at:
> Sebastian> i);flush]].
> Sebastian> 	results do:[:e| e value]
>
> I believe that this is a place where you need the fixTemps call on  
> the block.
>
> Did you try this as:
>
> 1 to: 2 do: [ :i |
>  results add: [
>    Transcript cr;
>       nextPutAll: (stuff at: Sebastian> i);flush
>       ] fixTemps
>  ].
>
> As in, notice the difference between:
>
> ((1 to: 3) collect: [:i | [i]]) collect: [:aBlock | aBlock value]
>
> => #(3 3 3)
>
> and
>
> ((1 to: 3) collect: [:i | [i] fixTemps]) collect: [:aBlock | aBlock  
> value]
>
> => #(1 2 3)
>
> -- 
> Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503  
> 777 0095
> <merlyn at stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
> Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
> See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl  
> training!
>

	Mth






More information about the Squeak-dev mailing list