[squeak-dev] Problem with a Collection of Blocks

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

[squeak-dev] Problem with a Collection of Blocks

Joel Turnbull

I'm trying to apply a collection of blocks to another collection to
filter it. like...

blockCollection := OrderedCollection new.
    collectionWith1and2 do: [ :tag | blockCollection add: [ :collection
| collection includes: tag ]].
self halt.

newCollection := collectionCollection copy.
blockCollection do: [ :block | newCollection := newCollection select:
block ].

It's not really behaving though. the problem is that blockCollection
doesn't get two different blocks referring two two different tags, but
two identical blocks pointing to second tag. I included a test method
below. I inserted a halt where if you explore blockCollection you can
drill down into home: and see that the date is 6 March 2009 on both
blocks in the collection.

Both tests fail, and I don't understand why :) If there's an alternative
strategy for applying multiple filters I'd love to hear it too.
Thanks, Joel

testUpdateCollectionFromBlocks
| newCollection |
   
    tag1 := Date year: 2009 month: 3 day: 5.
    tag2 := Date year: 2009 month: 3 day: 6.

    collectionWith1and2 := OrderedCollection new.
    collectionWith1and2 add: tag1; add: tag2.

    collectionWith2 := OrderedCollection new.
    collectionWith2 add: tag2.

    collectionCollection := OrderedCollection new.
    collectionCollection add: collectionWith1and2;    add: collectionWith2.
                       
    blockCollection := OrderedCollection new.
    collectionWith1and2 do: [ :tag | blockCollection add: [ :collection
| collection includes: tag ]].
    self halt.

    newCollection := collectionCollection copy.
    blockCollection do: [ :block | newCollection := newCollection
select: block ].
   
    self assert: ( newCollection = ( ( OrderedCollection new ) add:
collectionWith1and2 ) ).
    self deny: ( newCollection = collectionCollection ).



smime.p7s (6K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Problem with a Collection of Blocks

Julian Fitzell-2
On Thu, Mar 5, 2009 at 10:36 PM, Joel Turnbull <[hidden email]> wrote:

>
> I'm trying to apply a collection of blocks to another collection to filter
> it. like...
>
> blockCollection := OrderedCollection new.
>   collectionWith1and2 do: [ :tag | blockCollection add: [ :collection |
> collection includes: tag ]].
> self halt.
>
> newCollection := collectionCollection copy.
> blockCollection do: [ :block | newCollection := newCollection select: block
> ].
>
> It's not really behaving though. the problem is that blockCollection doesn't
> get two different blocks referring two two different tags, but two identical
> blocks pointing to second tag. I included a test method below. I inserted a

You should actually be getting two different blocks but they will all
be accessing the local variable in the same method context. This is a
problem caused by Squeak not having full block closures (though you
may have noticed discussion about some progress on this on the list in
the past few days).

Seaside fixes this by adding a method #fixCallbackTemps to
BlockContext which looks like:

fixCallbackTemps
        | temps |
        home := home copy.
        home swapSender: nil.
        home isMethodContext
                ifFalse: [ ^ self ].
        temps := self tempVarRefs.
        1 to: home size do: [ :index |
                (temps includes: index)
                        ifFalse: [ home tempAt: index put: nil ] ]

and calling it at appropriate places to avoid this problem. I'm not
certain what the suggested fix for non-Seaside users is at the moment.

Julian

Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Problem with a Collection of Blocks

Lukas Renggli
> and calling it at appropriate places to avoid this problem. I'm not
> certain what the suggested fix for non-Seaside users is at the moment.

There is #fixTemp that you can use in any image.

#fixCallbackTemps does some smart memory optimization useful in the
context of Seaside.

Lukas

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

Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Problem with a Collection of Blocks

Joel Turnbull

I am using Seaside and #fixCallbackTemps worked like magic. thanks guys.



smime.p7s (6K) Download Attachment