Blocks and it's temp vars

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Blocks and it's temp vars

Sebastian Sastre-2
Hi there,

        working with callbacks in Seaside I've found this code don't
evaluate properly in Squeak

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

        It complains about stuff array not having 3 elements (?!). Seems
that it has some problem with block temps. I saw a Seaside method trying to
separate the temps of the block from the ones of the method and at that
point the block temps was already malformed. The previous code is to expose
this problem.

        Tested on 3.9.final.7067 and 3.10 gamma.7159 (a couple of non Squeak
smalltalks works as expected).

        I need this feature badly so if anyone can give me any clue or
workaround would be great,

        thanks,

Sebastian


Reply | Threaded
Open this post in threaded view
|

Re: Blocks and it's temp vars

Randal L. Schwartz

[copied from Seaside list, where this was asked and answered as follows]

>>>>> "Sebastian" == Sebastian Sastre <[hidden email]> 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
<[hidden email]> <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!

Reply | Threaded
Open this post in threaded view
|

Re: Blocks and it's temp vars

Klaus D. Witzel
In reply to this post by Sebastian Sastre-2
This is a known consequence of the (so called old) compiler not doing  
BlockClosures.

The Seasiders often know that and make use of #fixTemps

  results add: [Transcript cr; nextPutAll: (stuff
at:i);flush] fixTemps

which eliminates this very problem problem :)

/Klaus

On Sat, 08 Dec 2007 20:08:02 +0100, Sebastian Sastre wrote:

> Hi there,
>
> working with callbacks in Seaside I've found this code don't
> evaluate properly in Squeak
>
> stuff := #(foo bar).
> results := OrderedCollection new.
> 1 to: 2 do:[:i|
> results add: [Transcript cr; nextPutAll: (stuff
> at:i);flush]].
> results do:[:e| e value].
>
> It complains about stuff array not having 3 elements (?!). Seems
> that it has some problem with block temps. I saw a Seaside method trying  
> to
> separate the temps of the block from the ones of the method and at that
> point the block temps was already malformed. The previous code is to  
> expose
> this problem.
>
> Tested on 3.9.final.7067 and 3.10 gamma.7159 (a couple of non Squeak
> smalltalks works as expected).
>
> I need this feature badly so if anyone can give me any clue or
> workaround would be great,
>
> thanks,
>
> Sebastian
>
>
>



Reply | Threaded
Open this post in threaded view
|

Re: Blocks and it's temp vars

Lukas Renggli
> The Seasiders often know that and make use of #fixTemps

#fixTemps is part of Squeak. Seasides mostly use #fixCallbackTemps,
what does the same as #fixTemps but also sets all the unused temps to
nil, what preserves some memory.

Lukas

--
Lukas Renggli
http://www.lukas-renggli.ch

Reply | Threaded
Open this post in threaded view
|

Re: Blocks and it's temp vars

Mathieu SUEN
In reply to this post by Randal L. Schwartz
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 <[hidden email]> 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
> <[hidden email]> <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




Reply | Threaded
Open this post in threaded view
|

RE: Blocks and it's temp vars

Sebastian Sastre-2
In reply to this post by Lukas Renggli
> #fixTemps is part of Squeak. Seasides mostly use
> #fixCallbackTemps, what does the same as #fixTemps but also
> sets all the unused temps to nil, what preserves some memory.
>
> Lukas
>
Good to know. Now I get why that method set nil in those temps.

I'll use it, thanks for the info,

cheers

Sebastian


Reply | Threaded
Open this post in threaded view
|

RE: Blocks and it's temp vars

Sebastian Sastre-2
In reply to this post by Mathieu SUEN
Hi Mathieu,

        seems necessary for the example I've exposed but not for in the code
that was giving me trouble. Anyway.. thanks for letting me know this
curification technique to help in this cases.

        cheers,

Sebastian Sastre


> -----Mensaje original-----
> De: [hidden email]
> [mailto:[hidden email]] En
> nombre de Mathieu Suen
> Enviado el: Sábado, 08 de Diciembre de 2007 21:41
> Para: The general-purpose Squeak developers list
> Asunto: Re: Blocks and it's temp vars
>
> 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 <[hidden email]> 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
> > <[hidden email]> <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
>
>
>
>