Code is an object

section of parrot logo 1section of parrot logo 2section of<br />
parrot logo 3section of parrot logo 4section of parrot logo 5section of parrot logo 6

In [Smalltalk has no "if" statement], I talked about how ifTrue:ifFalse: provides that functionality, and showed an example with two alternative blocks of code.

Now, ifTrue:ifFalse: is a message (a method call), and the code blocks are its parameters and, just like any other thing that can be passed as a parameter, that means that they are objects.

Since blocks are objects, you can expect to be able to send them messages. What kind of messages do they respond to? For a start, they respond to value, which is what ifTrue:ifFalse: sends to the selected block to run its code. They also understand fork, which forks a new process, and on:do:, which handles exceptions - but I’m getting ahead of myself.

Other ordinary-object things you might expect to be able to do with them include storing them in variables and inside other objects. That means that you can change the behaviour of an object by plugging in a new piece of code at run-time (the Pluggable Method pattern). Perl programmers may be nodding, because they can do that too. Wouldn’t it be nice if we could actually plug in brand-new code, though, and not just using some kind of eval, but as proper methods in classes like all the others?

If you’d like to be able to add brand-new code to your running application, then you’d better read: [No need to shut down].
If you want to know more about what you can do with blocks, then you could start with: [Non-local return].