View this PageEdit this PageAttachments to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide
Hotspots: Admin Pages | Turn-in Site |
Current Links: Cases Final Project Summer 2007

Heather Phillips

Coweb Assignment 2: Setting Up Monticello
Coweb Assignment 3

Message Passing:

What is message passing and how does it fit into the way you program in Smalltalk? How does message passing work together with the class-based inheritance? What are some of the advantages and disadvantages of method passing?

- Message passing is Smalltalk's way of telling objects what to do. Many different types of objects may be able to respond to the same message. Objects can respond to messages from their superclass and each object can respond to the message in a different way. An advantage of message passing is that you often don't have to test what type of object something is before calling a message on it because different objects respond to the same message. The disadvantage of this is that sometimes an object doesn't respond to a message the way you think it is going to or you may call a message on an object that doesn't take that message. In Smalltalk, there is no compiler to check if an object can receive a message. If you try to pass a message to an object that cannot receive it, you will get an error at runtime.

Tracing Code:

(Note: I have numbered the line for easier tracing)

"1"   | data onlyPositiveNumbers |
"2"   data := OrderedCollection withAll: #(1 2 3 -4 -5 'error' 6 -7 999 2).
"3"   onlyPositiveNumbers := [:i | (i isKindOf: Number) and: [i positive]].
"4"   data := data select: onlyPositiveNumbers.
"5"   data := data copyUpTo: 999. "not including"
"6"   Transcript show: data average


1: This line declares two temporary variables: data and onlyPositiveNumbers
2: data is given an OrderedCollection containing different types of objects, including strings and smallIntegers
It contains: (1 2 3 -4 -5 'error' 6 -7 999 2)
3: In line 3, onlyPositiveNumbers is given a block of code which loops through the values in its argument. For each value, it tests the type. If the type is a number, then whether it is positive is also tested. onlyPositiveNumbers filters its argument and returns only the positive numbers.
4: Here, the filter onlyPositiveNumbers is called on the OrderedCollection data and stored in data. data contains (1 2 3 6 999 2)
5: copyUpTo copies the values in data until it hits 999 (if the object is present, which it is). It stores the numbers back into data. Data now contains (1 2 3 6).
6: The average message is called on data and the numbers in data are averaged and the average is printed to the transcript.
-

Link to this Page