asBlock

View: New views
4 Messages — Rating Filter:   Alert me  

asBlock

by Steve Dekorte :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


If you've used a Call object you may have found that the code can get  
ugly when you deal with messages and contexts directly. So here's a  
method for Call that might help with this by wrapping the call into a  
block:

Call asBlock := method(
   args := self message arguments
   b := Block clone setMessage(args last) setScope(sender)
   b setArgumentNames(args slice(args size - 2) map(name))
   b
)

// example use:

List select := method(
    selected := List clone
    b := call asBlock
    self foreach(i, v,
      if(b call(i, v), selected append(v))
    )
    selected
)

list(1, 2, 3) select(i, v, v > 2) println

Which leads us to the question: Should Call and block be unified?


Re: asBlock

by Jeremy Tregunna-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Doesn't this increase the likelihood of a block becoming a GC root?  
And if so, is this still a problem in the GC where blocks may not be  
collected if they become part of the root set?

Regards,

Jeremy Tregunna
Mobile: +1 (519) 498-8299

Sent from my iPhone

On 2009-11-01, at 6:36 PM, Steve Dekorte <steve@...> wrote:

>
> If you've used a Call object you may have found that the code can get
> ugly when you deal with messages and contexts directly. So here's a
> method for Call that might help with this by wrapping the call into a
> block:
>
> Call asBlock := method(
>   args := self message arguments
>   b := Block clone setMessage(args last) setScope(sender)
>   b setArgumentNames(args slice(args size - 2) map(name))
>   b
> )
>
> // example use:
>
> List select := method(
>    selected := List clone
>    b := call asBlock
>    self foreach(i, v,
>      if(b call(i, v), selected append(v))
>    )
>    selected
> )
>
> list(1, 2, 3) select(i, v, v > 2) println
>
> Which leads us to the question: Should Call and block be unified?
>
>
>
> ------------------------------------
>
> Yahoo! Groups Links
>
>
>

Re: asBlock

by Steve Dekorte :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On 2009-11-01, at 3:42 PM, Jeremy Tregunna wrote:
> Doesn't this increase the likelihood of a block becoming a GC root?
> And if so, is this still a problem in the GC where blocks may not be
> collected if they become part of the root set?

I'm guessing you're thinking of how locals (and their chain of  
locals / "stack frames") referenced by a block won't be collected  
while the block is referenced. It's only a problem if you put the  
block in a slot of a object that is kept around. The same would happen  
if you referenced a call or the sender in the same way.

Re: asBlock

by Jeremy Tregunna-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Yes I was, thanks for the clarification.

On 2009-11-01, at 7:06 PM, Steve Dekorte wrote:

>
> On 2009-11-01, at 3:42 PM, Jeremy Tregunna wrote:
>> Doesn't this increase the likelihood of a block becoming a GC root?
>> And if so, is this still a problem in the GC where blocks may not be
>> collected if they become part of the root set?
>
> I'm guessing you're thinking of how locals (and their chain of
> locals / "stack frames") referenced by a block won't be collected
> while the block is referenced. It's only a problem if you put the
> block in a slot of a object that is kept around. The same would happen
> if you referenced a call or the sender in the same way.
>
>
> ------------------------------------
>
> Yahoo! Groups Links
>
>
>