Smalltalk Syntax Cheat Sheet
This page extracted from the "Smalltalk/X Programmers guide - Smalltalk/X Cheat Sheet"
A short, one page summary of what beginners need to know about Smalltalk/X syntax
Inhaltsverzeichnis
Syntax[Bearbeiten]
Comments[Bearbeiten]
Regular comment | " this is a comment "
|
any text between double quotes |
EOL-Comment | "/ this is an End-of-Line comment
|
text from quote-slash to line end |
Token-Comment | "<<END
|
text up to line beginning with token after ">>" |
Literal Constants[Bearbeiten]
Integers | 12345 -12345
|
|
Large integers | 1234567890123456789012345 …
|
arbitrary number of digits |
Integers with radix (Smalltalk style) |
16rAFFE , 2r010101 , 8r0777 , 3r012012 ,16r-AFFE , 2r-010101
|
prepend the radix (base) any radix possible |
Integers with radix (C style) |
0xFEED -0xBEAF 0b11011 ,-0b1100
|
C/Java/JavaScript style: prepend radix indicator ('x', 'b', 'o') One of hex, binary and octal |
Floats (IEEE double; roughly 17 decimals) |
12.456 1.234e17 -6.9 -6e-10 Float pi Float e |
called "Float", but actually hold double precision |
Short floats (IEEE single; roughly 8 decimals) |
1.234f17 -6f-10
|
|
Long floats (IEEE quad; roughly 20 decimals) |
1.234q17 -6q-10
|
actual precision depends on the Underlying CPU (on x86/x86_64: only 80bit) |
High prec. floats; roughly 60 decimals |
1.234Q17 -6Q-10
|
computed in Software; therefore slower) |
Fractions | (1/3) (17/2)
|
|
Characters | $a Character space Character nl Also: " null ", "tab ", "return ", "bell "...
|
|
Strings | 'hello'
|
traditionally, Smalltalk does not support any escape sequence inside |
Strings (C Style) |
c'hello\nworld'
|
supports the common escape sequences, such as "\n", "\t", "\xHH" |
Symbols | #'hello' #foo (without quotes, if only alphaNumeric characters) |
|
Arrays |
#( el1 el2 ... elN ) each element a literal constant |
|
Byte Arrays | #[ b1 b2 ... bN ] each byte-element an integer constant in 0..255 |
|
Special Number Arrays | #XX( v1 v2 ... vN ) XX: is one of 'u8', 's8', 'u16', 's16', 'u32', 's32', 'u64', 's64', 'f32', 'f64' and each element being an integer or float constant |
|
Immediate Inline Objects | #{ foo: fooValue . bar: barValue } fooValue and barValue: constant |
Lambda Blocks (Closures)[Bearbeiten]
Without argument | [ expr1 . expr2 ... exprN ]
|
One argument | expr1 . expr2 ... exprN ] |
Multiple arguments | expr1 . expr2 ... exprN ] |
Expressions[Bearbeiten]
the receiver expression is evaluated, then multiple messages (separated by ";") are sent to this receiver.
Unary Expression (without argument) | receiver messageName
receiver is itself either a constant, a lambda-block, a unary expression or a parenthesized expression |
Keyword Expression: (1 arg) | receiver messageNamePart:argExpression
whitespace between the colon and the argExpression is optional. |
Keyword Expression: (any number of args) | receiver
|
Binary Expression | receiver binOP arg
with binOP being any combination of special characters: * , + , - , % , & , / , \ , | , = , < , > , ? and ,
|
Cascade Expression (sending multiple messages to the same receiver) |
receiver |
Parentheses for grouping | ( any expression )
|
Assignment | variable := expression can be used as expression. |
Computed Array (Brace Construct) | { expr1 . expr2 . ... . exprN }
instantiates a new Array object with elements from the expressions. |
Computed Inline Objects |
{ foo: fooExpr . bar: barExpr }
|
Remaining Syntax[Bearbeiten]
Local variables (in block or method) | var1 var2 ... varN |
variable declarations must be at the beginning, before any expression. |
Separating multiple expressions (sequence) | expr1 . expr2 . ... exprN
expressions (statements) are separated by fullstop (period) characters.
|
Return from method | ^ expression
returns from the enclosing method (also if inside a block-closure) |
Wellknown (Common) Messages[Bearbeiten]
Conditional Execution | boolExpr1 ifTrue:[ ... ] ifFalse:[ ... ]
variations without true-part, without false part and with the order reversed are available. |
While-Loop | [ boolExpr1 ] whileTrue:[ ... ]
notice the receiver being a block. |
For-Loop | ... ]
evaluates the lambda for each value in start..stop. |
Enumerating Collections | ... ]
evaluates the lambda for each element in the collection. |
Evaluating a Lambda Block without arguments: with arguments: |
aBlock value
the number of arguments must match the number expected by the lambda (although varArg lambdas are also available) |
Wellknown Globals[Bearbeiten]
Logging and Messaging | Transcript - console window
|
Most Wellknown Classes[Bearbeiten]
Collections | Array (fixed size array)
|
Process Handling | Process (lightweight thread)OSProcess (heavyweight OS process)
|
Files & Streams | Filename (file naming, directory access, mime type)
|
Low Level Access | OperatingSystem (OS API calls)
|