Reputation: 86085
I heard that Smalltalk doesn't support local variable in blocks. Is this true? If it is, why doesn't Smalltalk support local variables? And can I still assume it as equal with closures?
Upvotes: 6
Views: 2706
Reputation: 10851
It depends on the smalltalk platform you choose. Basically you have block local variables on all smalltalk platforms. There are IMHO two kinds of implementations. If there isn't a full closure support the local variables are shared with the local variables of the method surrounding that block. For this you need to know how to work-around some problems. For full closure support local variables are there and work as you might expect.
Squeak and Pharo used to have locals that are shared with the method. Nowadays a VM with full closure support exists and Pharo supports this fully and I think Squeak does, too. I think gemstone hasn't full closure support. I don't know about VaST and VisualWorks.
You can always test it by doing something like the following:
((1 to: 5) collect: [:i|
[ | local | local := i ]])
collect: [:each| each value]
Here you only get
#(1 2 3 4 5)
if there is full closure support and
#(5 5 5 5 5)
if the | local | is shared with the method.
Upvotes: 13
Reputation: 3964
Do you mean a block local variable, like today
in this example:
10 timesRepeat:
[| today |
today := Date today.
Transcript cr; show: today printString]
Upvotes: 4
Reputation: 9340
huh? where did you hear that? try this code:
block := [
x := 10.
x printNl.
].
block value.
it should output 10.
Upvotes: 0