Usage of dot '.' in Smalltalk
Asked Answered
H

1

5

What exacly is the usage of . in Smalltalk? Based on my understanding, it is the separator of different statements, but can be omitted if statement is at the end. Is that correct?

Handbook answered 7/1, 2020 at 8:0 Comment(0)
M
8

The . is a statement separator like ; in Pascal (usually used at the end of lines). The motivation (reason) being that the ordinary sentences in English end with ..

The places it must/could be omitted are:

  1. Variable definition

  2. Comments

  3. One statement block or last statement at the block

  4. At the end of a method

  5. When you define a #selector or #selector: message

An example method from Smalltalk/X-jv:

selectorAsRegistryName: aSelector
    "Splits selector into string words with spaces. 
     For example: itemName becomes 'Item Name'"
    | registryName selectorCollection | 

    registryName := String new.

    selectorCollection := aSelector asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:ch | ch isUppercase ] withSeparatorsIncluded:true.             
    selectorCollection at: 1 put: selectorCollection copy first asUppercaseFirst. "/ first string must be uppercase too

    selectorCollection do: [ :eachString |
        registryName := registryName isEmpty ifTrue: [ eachString ]
                                            ifFalse: [ registryName, Character space, eachString ]   
    ].    

    ^ registryName       
Muriah answered 7/1, 2020 at 8:32 Comment(8)
I would not compare it to the ; in C because in C it is a statement terminator -- you cannot omit the ; after the last statement in C. But in Pascal/Delphi you can omit the ; at the end.Passkey
@Passkey true. Pascal is closer to the . logic. I'll edit the answer, thank you for improving the answer.Muriah
Coming from C++/Python/JavaScript, I find it hard to remember when to put . and when not to. Specifically for your 5. Selector name or message, could you elaborate more? Also, is there a tool that can inform(or help) you check the syntax? Thanks.Handbook
@laike9m: a selector selectorAsRegistryName: a message here aSelector (method name and its parameter in other languages). I would start putting the . everywhere where it should be. You don't need a special tool - your code will be checked if it is correct when you accept your code. (At workspace where you can play with your code (select it and Do it) you will get an error when incorrect).Muriah
Thanks tukan. Actually I know what a selector and message is, but I don't quite understand what you mean by . can be omitted in this case. For example, in your code snippet, String new. has dot, and it cannot be omitted.Handbook
@Handbook Ah, I see. What I meant is right after e.g #selectorAsRegistryName or as stated in the example #selectorAsRegistryName: aSelector. (the method name and parameter). You don't write neither selectorAsRegistryName. nor selectorAsRegistryName: aSelector. . Within the the method it as shown in the example you end a statement.Muriah
I see, so it means "dot can be omitted when you define a message" if I understand correctly.Handbook
@Handbook yes,precisely. I'll edit the answer for better understading.Muriah

© 2022 - 2024 — McMap. All rights reserved.