[Newbies] Block and Closure

Lukas Renggli renggli at gmail.com
Fri Apr 10 20:41:56 UTC 2009


> Why is this particularly hard for smalltalk? Scheme implementations
> manage to do it. Is it simply that it's an afterthought, when it needs
> to be designed in from the ground up?

Two common problems I observed are related to:

(1) Smalltalk inlines some blocks, such as the ones involved in
conditionals and loops. Even these loop blocks are no real blocks
anymore, they should behave exactly like real ones. For example the
following 2 expression should yield the same result #(1 2 3 4 5) when
evaluating the blocks in the collection:

     " in this case the block is inlined "
     1 to: 5 do: [ :index | collection add: [ index ] ]

     " in this case the block is not inlined "
     (1 to: 5) do: [ :index | collection add: [ index ] ]

(2) Some closure implementations perform invalid optimizations on
blocks and wrongly copy enclosed values when they think they are not
written to after block creation (AFAIK). In some cases this results in
very strange effects, such as that the following two expressions are
not symmetrical. In both cases the result should be #(5 5 5 5 5).

	| index |
	index := 0.
	[ index < 5 ] whileTrue: [
		collection add: [ index ].
		index := index + 1 ].

	| index |
	index := 0.
	[ index < 5 ] whileTrue: [
		index := index + 1.
		collection add: [ index ] ].

Cheers,
Lukas

-- 
Lukas Renggli
http://www.lukas-renggli.ch


More information about the Beginners mailing list