Sunday, July 22, 2007

messages are different in Smalltalk

I'm having a look a Smalltalk / Squeak programming. It's promoted as object oriented (OOP) in a pure form, so my intuition is that if I can grok Smalltalk then that will deepen my understanding of OOP and how it is really meant to work.
Early Smalltalk was the first complete realization of these new points of view as parented by its many predecessors in hardware, language and user interface design. It became the exemplar of the new computing, in part, because we were actually trying for a qualitative shift in belief structures--a new Kuhnian paradigm in the same spirit as the invention of the printing press-and thus took highly extreme positions which almost forced these new styles to be invented
- Alan Kay, The Early History of Smalltalk
  • Everything is an object.
  • All computation is triggered through message sends. You send a message to an object, and something happens.
  • Almost all executable Smalltalk expressions are of the form <receiverobject> <message>.
  • Messages trigger methods where the mapping of message-to-methods is determined by the receiving object. Methods are the units of Smalltalk code.
  • - Chapter 2. A Tour of Squeak, in: Squeak: Object-Oriented Design with Multimedia Applications by Mark Guzdial
Understanding messages is crucial:
Kay has repeatedly expressed his regret that he chose the term “object-oriented” instead of the more relational concept of “message-oriented.” What is important about biological cells in Kay’s systems theory rendering of them isn’t what they’re made of, but rather their modes of interacting.
- Tracing the Dynabook by John Maxwell, p. 121

This first one is from Stephane Ducasse's book on learning squeak through programming robots and the others are from Mark Guzdial's book cited above. Comments are in "quotes".

| pica | "local variable declared"
pica := Bot new. "The new message is sent to the Bot class to create a new robot and associate with it the name pica (:= is for assignment)"
pica go: 100 "the colon (:) after go means that an argument is required. go: 100 is a message send to the robot object pica, which can also be described as the message receiver"

1 to: 10 do: [instruction block]

1 to: 10 do: [] is a message send to the object 1! The message to: do: is a message understood by the Integer class! 10 and the block of code (statements contained in square brackets) following do: are actually arguments in the message.

1 to: 10 do: [:i | Transcript show:(i printString), ' times'; cr]

do: evaluates the block, :i defines the index variable for the loop, the vertical bar separates the definition from the rest of the statement, printString sends a message to i, the Transcript is a separate window, the comma (,) means concatenate, the semi-colon is used to string statements together and cr stands for carriage return. The output to the Transcript window is:
1 times
2 times
3 times
4 times
5 times
6 times
7 times
8 times
9 times
10 times

anArray := Array new: 10. "create a new array with 10 places"
#(nil nil nil nil nil nil nil nil nil nil)

| aValue | "declare a variable"
aValue := 2.
Everything is an object. This rule does not actually mean "Set the value of 'aValue' to integer 2" but instead means "Set the variable aValue to point to an SmallInteger object whose value is 2."

1 to: 10 do: [:index | anArray at: index put: aValue*index ].

Transcript show: anArray

Output to Transcript window:
#(2 4 6 8 10 12 14 16 18 20)

This has given me some understanding of how messages are both important and different in Smalltalk.


Stinckwich said...

You forget something important about messages and object: when you send a message to an object, this object reply with another object.

Bill Kerr said...

hi serge,

I thought I had covered that by stating that everything in Smalltalk is an object. Is there some significance that I am missing here?