Re: Difference between method and block - with examples

Jan-Paul Bultmann <[email protected]>
Newsgroups gmane.comp.lang.io
Message-ID <[email protected]>
I don't think these explanations resolve the Problem of Shashank. Why the heck is the stuff in my Closure changing!?
The Closures or Blocks in Io bind their environment by reference and thus all the variables in it by reference.
What you probably expect is, that it binds them by value.(Python shows this behavior AFAIK) So once the Closure is created all the variables referenced in it will stay the same.
Besides reasons I can't answer, I would guess dynamism, Io has to do it this way, because Methods and Blocks are both Block objects. Only the Initialization differs slightly, for methods the target will be the object you assign it to, while for blocks it will be the object you created it in.

And even more importantly, when you crate the Block you have no access to the outer namespace, only when it is called.
Like 
n := 1
A := Object clone do(a := n)

This will throw you an exception because Object doesn't have a "n" Slot.
So one of the greatest strengths of Io is one of its greatest weaknesses, be darn careful where your expression gets evaluated!

So to do what you want you have to put the slots into the locals object before the block is activated.

Hey, ML how to get the locals Object :D before activation?
Could it be useful to have direct access to it?

a := 5
b := block(v, n + v) localsDo(n := a)

Cheers Jan

On Jun 15, 2011, at 9:47 AM, Steve Dekorte wrote:
> 
> On 2011-06-14 Tue, at 11:44 PM, Oscar Martinez wrote:
> > n := 10
> > mthd := method(i, i + n)
> > blk := block(i, i + n)
> > foo := method(n = 20; mthd(3))
> > bar := method(n = 30; blk)
> > 
> > mthd(1) println # 11 (as expected)
> > blk call(2) println # 12 (as expected)
> > foo println # 23 (as expected)
> > bar call(4) println # 34 (but really expected 14)
> > 
> > Actually, 34 is what you are getting because in "bar" you are updating the existing "n" slot :)
> 
> Right. 
> 
> n = 20 
> 
> is compiled as:
> 
> updateSlot("n", 20)
> 
> and the Locals proto (which method's basically clone to use as a locals scope) overrides updateSlot to have a slightly different behavior in that it sets it on self if the slot is found there. I don't know if this is clearly described in the docs but the motivation is to being able to write:
> 
> n = 20 
> 
> instead of:
> 
> self n = 20
> 
> I'm not sure if that was the right choice.
> 
Why, this makes totally sense!  := creates a variable in the local namespace, while = updates the first one it finds crawling up the namespaces.
What good would be normal inheritance here?^^
> It isn't done for :=.
> 
> 

> - Steve
> 
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.