[Newbies] Modify block (closure) parameters

psea denis.lukichev at gmail.com
Mon Aug 5 08:44:15 UTC 2013


Hi Smalltalkers! 

Here is a question I can't find answer with google. 
In short: Does block parameters and block local variables are the same thing
(semantically) inside closure? It looks like it's not. Below the
explanation. 

=================== 
Here is the accumulator function (first attempt): 

makeAcc := [ :acc |   
        [:n | acc:=acc+n. acc]] 

It turns out it doesn't work as intended: 

a1 := makeAcc value: 10 
a1 value: 1 => 11 
a1 value: 1 => 11 
a1 value: 2 => 12 

==================== 
Here is the second attempt: 

makeAcc := [ :acc |   
        | total | 
        total:=acc. 
        [:n | total:=total+n. total]] 

And it does work as intended: 

a1 := makeAcc value: 10 
a1 value: 1 => 11 
a1 value: 1 => 12 
a1 value: 2 => 14 

So if we use the local variable to store accumulator it works as it should
and remembers the value between function (block) calls. But if we use block
parameter to store the value of accumulator it does not remembers the value
between calls. 
Why is it so? 
 
P.S. Sorry for my English. I do all my best.



--
View this message in context: http://forum.world.st/Modify-block-closure-parameters-tp4702118.html
Sent from the Squeak - Beginners mailing list archive at Nabble.com.


More information about the Beginners mailing list