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

I heard that Smalltalk does not support a local variable in blocks. It's true? If so, why does Smalltalk not support local variables? And can I consider it equal to closing?

+4
source share
4 answers

It depends on the smalltalk platform you choose. Basically, you have local variable locking on all smalltalk platforms. There are IMHO two types of implementations. If there is no full support for closure, local variables are shared by local variables of the method surrounding this block. To do this, you need to know how to solve some problems. To fully support closure, local variables exist and work as you might expect.

Squeak and Pharo used locales that are shared by this method. There is currently a virtual machine with full closure support, and Pharo fully supports it, and I think Squeak too. I think the gem does not have full closure support. I do not know about VaST and VisualWorks.

You can always check it by doing the following:

((1 to: 5) collect: [:i| [ | local | local := i ]]) collect: [:each| each value] 

Here you get

 #(1 2 3 4 5) 

if there is full support for closing and

 #(5 5 5 5 5) 

if | local | shared with the method.

+10
source

You mean the local variable of the block, for example today in this example:

 10 timesRepeat: [| today | today := Date today. Transcript cr; show: today printString] 
+2
source

a? where did you hear that? try this code:

 block := [ x := 10. x printNl. ]. block value. 

he should output 10.

0
source

Yes, VA Smalltalk supports them.

0
source

All Articles