Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Smalltalk
(section)
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".
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)