Messages
SenseTalk is an object-oriented language that works by passing messages from one object to another. When a message is sent to an object, it can respond if its script has a handler for that message. If the object does not have a handler for a particular message, that message is passed along to some other object. For more information on working with handlers, see Handlers.
Objects do things only when they receive a message. An object can send messages to itself, or they may be sent to it by another object, or by the environment in which the script resides. For example, when a script is invoked from the command line, it is sent a message based on the name of the script, which causes the script to run.
To begin with, all you need to know is that when a message is sent to an object, it can either receive and handle that message, or it can ignore it. Continue reading to look at messages and how they are sent.
Sending Messages
When a script is running, it sends messages constantly as it executes its commands. A message is always a single word, and the simplest message-sending statement is a single-word command, which is the name of the message to be sent. For example, the second line of this script sends the message “greetTheUser”:
put "Hello, World!"
greetTheUser
put farewellMessage()
In this script, the put
commands on the first and third lines also send messages. In fact, with the exception of a few fundamental SenseTalk control structures, almost every command in a script sends a message. Function calls, such as the call to the “farewellMessage()” function above, also send messages.
Command Messages and Function Messages
SenseTalk actually sends two different types of messages. Command messages are sent by commands, such as greetTheUser
in the example above. Function messages are sent by function calls, such as farewellMessage()
in the example above. The two message types are identical except for the type of handler that will receive them (see Handlers). For more information on how to call functions, see Functions. For more information on how to use commands, see Commands and Comments.
Where are Messages Sent?
We said earlier that messages are used to allow objects to communicate with one another, so you might be wondering where the “greetTheUser” and “put” messages above are being sent. What object will receive these messages? The answer is very simple, though perhaps somewhat surprising: the object containing these commands will send these messages to itself!
While at first it may not appear to be terribly useful for an object to send messages to itself, in practice any message which is not handled by the object is passed along for possible handling by other objects or ultimately by one of SenseTalk’s built-in commands or functions.
Messages can also be sent directly to some other particular object, if desired. This is done using the tell
command, send
command, run
command, run
function, the square-bracket messaging syntax, or by using of
, .
or 's
like this:
put the salary of writer into authorPay -- sends "salary" to writer
get investor's balance("Checking") -- sends "balance" to investor
add paycheck.netIncome() to it -- sends "netIncome" message to paycheck