smalltalk Smalltalk Syntax Literals and comments


Example

Coments

"Comments are enclosed in double quotes. BEWARE: This is NOT a string!"
"They can span
 multiple lines."

Strings

'Strings are enclosed in sigle quotes.'
'Single quotes are escaped with a single quote, like this: ''.'
''''  "<--This string contains one single quote"

'Strings too can span
multiple lines'

''   "<--An empty string."

Symbols

#thiIsaSymbol  "Symbols are interned strings, used for method and variable names,
                and as values with fast equality checking."
#'hello world' "A symbol with a space in it"
#''            "An empty symbol, not very useful"
#+
#1             "Not the integer 1"

Characters

$a     "Characters are not strings. They are preceded by a $."
$A     "An uppercase character"
$      "The spacecharacter!"
$→     "An unicode character"
$1     "Not to be confused with the number 1"

Numbers

Numbers come in all varieties:

Integers:

  • Decimal

    10

    -1

    0

    1000000000000000000000000000000000000000000

  • Hexadecimal

    16rAB1F

    16r0

    -16rFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

  • ScaledDecimal

    17s0

    3.14159265s8

  • Other

    8r7731 "octal"

    2r1001 "binary"

    10r99987 "decimal again!"

Floating point

3.14 1.2e3     "2 floating-point numbers"

Fractions

Fractions are not the same as floating-point numbers, they're exact numbers (no rounding error).

4/3            "The fraction 4/3"
355/113        "A rational approximation to pi"

Arrays

#(#abc 123)    "A literal array with the symbol #abc and the number 123"

Byte Arrays

#[1 2 3 4]       "separators are blank"
#[]              "empty ByteArray"
#[0 0 0 0 255]   "length is arbitrary"

Dynamic arrays

Dynamic arrays are built from expressions. Each expression inside the braces evaluates to a different value in the constructed array.

{self foo. 3 + 2. i * 3}   "A dynamic array built from 3 expressions"

Blocks

[ :p | p asString ] "A code block with a parameter p.
                     Blocks are the same as lambdas in other languages"

Some notes:

Note that literal arrays use any kind and number of blanks as separators

#(256 16rAB1F 3.14s2 2r1001 $A #this)
"is the same as:"
#(256
  16rAB1F
  3.14s2
  2r1001
  $A #this)

Note also that you can compose literals

#[255 16rFF 8r377 2r11111111]       (four times 255)

#(#[1 2 3] #('string' #symbol))     (arrays of arrays)

There is some "tolerance" to relaxed notation

#(symbol) = #(#symbol)              (missing # => symbol)

#('string' ($a 'a'))                (missing # => array)

But not here:

#([1 2 3]) ~= #(#[1 2 3])           (missing # => misinterpreted)

However

#(true nil false)                   (pseudo variables ok)

But not here!

#(self) = #(#self)                  (missing # => symbol)

As you can see there are a couple of inconsistencies:

  • While pseudo variables true, false and nil are accepted as literals inside arrays, the pseudo variables self and super are interpreted as Symbols (using the more general rule for unqualified strings.)

  • While it is not mandatory to write #( for starting a nested array in an array and the parenthesis suffices, it is mandatory to write #[ for starting a nested ByteArray.

Some of this was taken from:

http://stackoverflow.com/a/37823203/4309858