Navigation:  Tutorials > Beginners Guide to Smalltalk > Objects and Messages >

Keyword Messages

Previous pageReturn to chapter overviewNext page

We have seen above that binary messages take a single parameter. Binary messages are special characters such as: + * - /etc. It is also possible to supply parameters to the more alphabetical style of message selectors.

#(100 600 800 1000) at: 2.

#(100 200 #( 1 2 3) 800 1000) at: 3

 

As shown above, the at: message can be used to access an object at a particular index within an array, or other indexable collection. In the second example, you see that you can have quite complex arrays which even include other arrays as their elements. This makes sense since an array can easily hold any type of object. Here are some more examples of keyword messages that take a single parameter:

#(100 200 #( 1 2 3) 900 1000) includes: 30 squared

'Hello World' occurrencesOf: $o

1 to: 10

 

The latter is another example of a constructor message similar to @. In this case to: sent to an integer will answer a special sort of collection known as an Interval. Intervals can be used where any other collection can be used and this one represents the range of integer numbers between one and ten. To see this better, ask it to convert itself to an array:

(1 to: 10) asArray

 

So far, we have seen how single parameters can be passed to messages ending in a :. Messages can also be created that take any number of parameters:

(1 to: 3) at: 2 put: 'Hello'; yourself

 

We'll come back to the use of the semicolon and yourself message in the section on Cascading Messages below. For the time being concentrate on the at: and put:. These are not two separate message sends but one single one that takes two parameters. We refer to the selector for this keyword message as at:put:. As you can see, this at:put: message can be used to store objects into indexable collections such as arrays or strings. Try these:

'Thomas,Henry,Edward' copyFrom: 8 to: 12

'Thomas,Henry,Edward' midString: 5 from: 8

'The brown quick fox' replaceFrom: 5 to: 15 with: 'a quick brown' startingAt: 3

 

Can you say what the message selector in the last example is? Yes, it's a single message that takes four parameters and has the selector, replaceFrom:to:with:startingAt:.

Tip: I hope you can see that this way that Smalltalk has, of interspersing parameters within the body of the message selector, can lead to much more readable program code than with most other languages. If you are used to languages such as C++ or Java you may find this odd at first but I'd wager that, pretty soon, you'll come to like it!

'Hello World' at: 6 put: $-; yourself

 

Try the above. Here you get an error, "Attempt to update read-only object". This is because you are trying to directly modify the contents of a literal string object created by the compiler. Since the compiler likes to be able to share strings if they are identical, it is not usually safe to allow them to be modified.  The following, however, will work just fine:

'Hello World' copy at: 6 put: $-; yourself