Re: Difference between method and block - with examples

Steve Dekorte <[email protected]>
Newsgroups gmane.comp.lang.io
Message-ID <[email protected]>
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. 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.