Skip to main content
Engineering LibreTexts

4.4: Hints for Identifying Keyword Messages

  • Page ID
    36351
  • \( \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}}\)

    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: rotatedBy:magnify:smoothing: and at:put:.

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

    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.

    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 hence is sent prior to the keyword message ifTrue:.

    (x isNil)
        ifTrue:[...]
    

    The following piece of 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 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 evaluated. [expression] will create a block closure (i.e., an object) from expression, which may be evaluated any number of times (possibly zero), depending on the context. Here note that an expression can either be a message send, a variable, a literal, an assignment or a block.

    Hence the conditional branches of ifTrue: or ifTrue:ifFalse: require blocks. Following the same principle both the receiver and the argument of a whileTrue: message require the use of square brackets since we do not know how many times either the receiver or the argument should be evaluated.

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

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

    This page titled 4.4: Hints for Identifying Keyword Messages is shared under a CC BY-SA 3.0 license and was authored, remixed, and/or curated by Andrew P. Black, Stéphane Ducasse, Oscar Nierstrasz, Damien Pollet via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.