Re: Difference between method and block - with examples

Oscar Martinez <[email protected]>
Newsgroups gmane.comp.lang.io
Message-ID <[email protected]>
2011/6/15 Shashank <[email protected]>

>
>
> Hi,
>
>
> --- In [email protected], Tiogshi Laj <tiogshi@...> wrote:
> >
> > That doesn't work because := creates a new slot with that binding if one
> > doesn't exist in lexical scope. You want to use just = there in order to
> > assign to the inherited slot instead of creating a new one.
> >
>
> That *almost* worked, except one case:
>
>
> 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 :)


> Basically, I am expecting that method() uses local variables visible in the
> context of the caller (hence dynamic closure) and block() uses local
> variables visible in the context of its definition (hence static closure).
>
> May be my expectations are tinted by my understanding of static vs. dynamic
> closures. Or the lack thereof :)
>

I think the explanation is in the documentation:

"A method is an anonymous function which, when called, creates an object to
store its locals and sets the local's proto pointer and its self slot to the
target of the message"

So, a method can access his locals slots and the ones from the object who
activated it.

Regards.
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.