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!
===Expressions=== Smalltalk is an [[Expression-oriented programming language|expression-based language]]. Every statement, including control constructs, has a value, which is some object. An expression can include multiple message sends. In this case expressions are parsed according to a simple order of precedence. Unary messages have the highest precedence, followed by binary messages, followed by keyword messages. For example: <syntaxhighlight lang="smalltalk">3 factorial + 4 factorial between: 10 and: 100</syntaxhighlight> is evaluated as follows: #3 receives the message "factorial" and answers 6 #4 receives the message "factorial" and answers 24 #6 receives the message "+" with 24 as the argument and answers 30 #30 receives the message "between:and:" with 10 and 100 as arguments and answers true The answer of the last message sent is the result of the entire expression. Parentheses can alter the order of evaluation when needed. For example, <syntaxhighlight lang="smalltalk">(3 factorial + 4) factorial between: 10 and: 100</syntaxhighlight> will change the meaning so that the expression first computes "3 factorial + 4" yielding 10. That 10 then receives the second "factorial" message, yielding 3628800. 3628800 then receives "between:and:", answering false. Because the meaning of binary messages is not coded into Smalltalk-80 syntax, all of them are considered to have equal precedence and are evaluated simply from left to right. Because of this, the meaning of Smalltalk expressions using binary messages can be different from their "traditional" interpretation: <syntaxhighlight lang="smalltalk">3 + 4 * 5</syntaxhighlight> is evaluated as "(3 + 4) * 5", producing 35. To obtain the expected answer of 23, parentheses must be used to explicitly define the order of operations: <syntaxhighlight lang="smalltalk">3 + (4 * 5)</syntaxhighlight> Unary messages can be ''[[method chaining|chained]]'' by writing them one after another: <syntaxhighlight lang="smalltalk">3 factorial factorial log</syntaxhighlight> which sends "factorial" to 3, then "factorial" to the result (6), then "log" to the result (720), producing the result 2.85733. A series of expressions can be written as in the following (hypothetical) example, each separated by a period (period is a statement separator, not a statement terminator). This example first creates a new instance of class Window, stores it in a variable, and then sends two messages to it. <syntaxhighlight lang="smalltalk"> | window | window := Window new. window label: 'Hello'. window open </syntaxhighlight> If a series of messages are sent to the same receiver as in the example above, they can also be written as a ''[[method cascading|cascade]]'' with individual messages separated by semicolons: <syntaxhighlight lang="smalltalk"> Window new label: 'Hello'; open </syntaxhighlight> This rewrite of the earlier example as a single expression avoids the need to store the new window in a temporary variable. According to the usual precedence rules, the unary message "new" is sent first, and then "label:" and "open" are sent to the receiver of "new".
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