Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Smalltalk
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Messages=== The message is the most fundamental language construct in Smalltalk. Even control structures are implemented as [[message passing|message sends]]. Smalltalk adopts by default a [[dynamic dispatch]] and [[single dispatch]] strategy (as opposed to [[multiple dispatch]], used by some other object-oriented languages). There are three kinds of message sends, unary messages which have a single keyword, such as <syntaxhighlight lang="smalltalk" inline>class</syntaxhighlight> and <syntaxhighlight lang="smalltalk" inline>size</syntaxhighlight>, binary messages which for example are used for arithmetic, such as <syntaxhighlight lang="smalltalk" inline>a < b</syntaxhighlight>, <syntaxhighlight lang="smalltalk" inline>a ~= b</syntaxhighlight>, and keyword messages where a keyword followed by a colon precedes each argument in the message, so that <syntaxhighlight lang="smalltalk" inline>a between: b and: c</syntaxhighlight> sends the <syntaxhighlight lang="smalltalk" inline>#between:and:</syntaxhighlight> message to <code>a</code> with arguments <code>b</code> and <code>c</code>. Unary messages have higher precedence than binary messages, which have higher precedence than keyword messages, and evaluation is strictly left-to-right. There is no arithmetic precedence. <syntaxhighlight lang="smalltalk" inline>1 + 2 * 3</syntaxhighlight> evaluates to 9, not to 7. The following example sends the message 'factorial' to number 42: <syntaxhighlight lang="smalltalk">42 factorial</syntaxhighlight> In this situation 42 is called the message ''receiver'', while 'factorial' is the message ''selector''. The receiver responds to the message by returning a value (presumably in this case the [[factorial]] of 42). Among other things, the result of the message can be assigned to a variable: <syntaxhighlight lang="smalltalk">aRatherBigNumber := 42 factorial</syntaxhighlight> "factorial" above is what is called a ''unary message'' because only one object, the receiver, is involved. Messages can carry additional objects as ''arguments'', as follows: <syntaxhighlight lang="smalltalk">2 raisedTo: 4</syntaxhighlight> In this expression two objects are involved: 2 as the receiver and 4 as the message argument. The message result, or in Smalltalk parlance, ''the answer'' is supposed to be 16. Such messages are called ''keyword messages''. A message can have more arguments, using the following syntax: <syntaxhighlight lang="smalltalk">'hello world' indexOf: $o startingAt: 6</syntaxhighlight> which answers the index of character 'o' in the receiver string, starting the search from index 6. The selector of this message is "indexOf:startingAt:", consisting of two pieces, or ''keywords''. Such interleaving of keywords and arguments is meant to improve readability of code, since arguments are explained by their preceding keywords. For example, an expression to create a rectangle using a C++ or Java-like syntax might be written as: <syntaxhighlight lang="java">new Rectangle(100, 200);</syntaxhighlight> It's unclear which argument is which. By contrast, in Smalltalk, this code would be written as: <syntaxhighlight lang="smalltalk">Rectangle width: 100 height: 200</syntaxhighlight> The receiver in this case is "Rectangle", a class, and the answer will be a new instance of the class with the specified width and height. Finally, most of the special (non-alphabetic) characters can be used as what are called ''binary messages''. These allow mathematical and logical operators to be written in their traditional form: <syntaxhighlight lang="smalltalk">3 + 4</syntaxhighlight> which sends the message "+" to the receiver 3 with 4 passed as the argument (the answer of which will be 7). Similarly, <syntaxhighlight lang="smalltalk">3 > 4</syntaxhighlight> is the message ">" sent to 3 with argument 4 (the answer of which will be false). The programmer is free to define new binary selectors just as they are free to define novel unary and keyword messages. Notice, that the Smalltalk-80 language itself does not imply the meaning of those operators. The outcome of the above is only defined by how the receiver of the message (in this case a Number instance) responds to messages "+" and ">". A side effect of this mechanism is [[operator overloading]]. A message ">" can also be understood by other objects, allowing the use of expressions of the form "a > b" to compare them.
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Smalltalk
(section)
Add topic