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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With