Mismatched brackets or parentheses
Incorrectly placed or missing brackets and parentheses are also common errors. For example, see what happens when you evaluate the following:
| x y |
x := 9.
[x > 0
ifTrue: [y := 'Positive']
ifFalse: [y := 'Negative']].
Transcript cr; show: y printString
Now, remove the outer brackets and evaluate it again. The first expression prints a warning message to the Transcript and indicates that y was nil. The second expression prints 'Positive' to the Transcript.
Tips
Double-clicking just inside a , (, {, or " highlights the text enclosed by that character and its matching character.
Note that printing to the Transcript offers another way of viewing an object's state, and of determining whether your code contains errors.
Whenever you use parentheses, you need to keep in mind how parentheses affect the order in which the system evaluates code. The system evaluates code left to right. It evaluates expressions within parentheses first, starting with the left-most inner parentheses. For example, display
| x |
x := 3 * 2 + (4 - 1)
Now compare the 9 returned to the number returned when you display an expression that adds a second pair of parentheses:
| x |
x := 3 * (2 + (4 - 1))
Last modified date: 05/19/2020