Skip to main content
Engineering LibreTexts

5.4: Hints for Identifying Keyword Messages

  • Page ID
    39594
    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Often beginners have problems understanding when they need to add parentheses. Let’s see how keywords messages are recognized by the compiler.

    Parentheses or not?

    The characters [, ], ( and ) delimit distinct areas. Within such an area, a keyword message is the longest sequence of words terminated by : that is not cut by the characters ., or ;. When the characters [, ], ( and { surround some words with colons, these words participate in the keyword message local to the area defined.

    In this example, there are two distinct keyword messages: rotateBy:magnify:smoothing: and at:put:.

    aDict
        at: (rotatingForm
            rotateBy: angle
            magnify: 2
            smoothing: 1)
        put: 3
    

    Note

    The characters [, ], ( and ) delimit distinct areas. Within such an area, a keyword message is the longest sequence of words terminated by : that is not cut by the characters ., or ;.When the characters [, ], ( and ) surround some words with colons, these words participate in the keyword message local to the area defined.

    Precedence hints

    If you have problems with these precedence rules, you may start simply by putting parentheses whenever you want to distinguish two messages having the same precedence.

    The following piece of code does not require parentheses because the message send x isNil is unary, and is sent prior to the keyword message ifTrue:.

    (x isNil)
        ifTrue: [...]
    

    The following code requires parentheses because the messages includes: and ifTrue: are both keyword messages.

    ord := OrderedCollection new.
    (ord includes: $a)
        ifTrue: [...]
    

    Without parentheses the unknown message includes:ifTrue: would be sent to the collection!

    When to use [ ] or ( )

    You may also have problems understanding when to use square brackets (blocks) rather than parentheses. The basic principle is that you should use [ ] when you do not know how many times, potentially zero, an expression should be executed. [ expression ] will create a block closure (an object, as always) from expression, which may be executed zero or more times, depending on the context. (Recall from Chapter : Syntax in a Nutshell that an expression can either be a message send, a variable, a literal, an assignment or a block.)

    Following this principle, the conditional branches of ifTrue: or ifTrue:ifFalse: require blocks. Similarly, both the receiver and the argument of the whileTrue: message require the use of square brackets since we do not know how many times either the receiver (the loop conditional) or the argument (the ”loop body”) will be executed.

    Parentheses, on the other hand, only affect the order of sending messages. So in ( expression ), the expression will always be executed exactly once.

    "both the receiver and the argument must be blocks"
    [ x isReady ] whileTrue: [ y doSomething ]
    
    "the argument is evaluated more than once, so must be a block"
    4 timesRepeat: [ Beeper beep ]
    
    "receiver is evaluated once, so is not a block"
    (x isReady) ifTrue: [ y doSomething ]
    

    This page titled 5.4: Hints for Identifying Keyword Messages is shared under a CC BY-SA 3.0 license and was authored, remixed, and/or curated by via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.