Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Smalltalk support local variable in blocks? If not, why?

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?

like image 471
eonil Avatar asked Jun 14 '11 08:06

eonil


1 Answers

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.

like image 91
Norbert Hartl Avatar answered Sep 30 '22 12:09

Norbert Hartl