Navigation:  Appendix A: Jumping Into Smalltalk >

Blocks

Previous pageReturn to chapter overviewNext page

Smalltalk has an object called a block. It's an object that contains executable code. The closest thing Java has is an anonymous inner class. In C# 2.0, there are anonymous delegates that are similar.

To execute a block with no parameters, you send it a value message.

Smalltalk

| block |
block := [3 + 4].
block value "answer is 7"

Blocks can have parameters. Each block parameter declaration starts with a colon (:). A vertical bar (|) denotes the end of the parameter list and the start of the code for the block.

Smalltalk

| block |
block := [:x :y | x * 2 + y].
block value: 5 value: 3 "answer is 13"