Smalltak
Certainly! Below is a syntax breakdown for Smalltalk, covering its fundamental syntactic elements. Smalltalk is a minimalist, dynamically typed, purely object-oriented language with a highly uniform syntax.
Smalltalk Syntax Breakdown
1. Comments
- Single-line comments use double quotes.
"This is a comment in Smalltalk"
2. Variables
- Local variables (method-scoped) are declared at the start of a method inside
| |.| x y z | "Declares three variables: x, y, z" - Instance variables (object fields) are declared in the class definition (not in methods).
- Global variables (shared across the system) start with uppercase letters (e.g.,
Transcript). - Temporary variables (block-scoped) are declared similarly to locals but inside blocks.
3. Assignments
- Assignment uses
:=.x := 10. "Assigns 10 to x"
4. Messages (Method Calls)
Smalltalk uses message passing for all operations. There are three types of messages:
- Unary messages (no arguments):
array size "Sends 'size' message to 'array'" - Binary messages (one argument, symbolic):
3 + 4 "Sends '+' with argument 4 to 3" - Keyword messages (multiple arguments, colons
:):array at: 1 put: 'hello' "Sends 'at:put:' with args 1 and 'hello'"
5. Method Definitions
Methods are defined inside classes. Syntax:
methodName
"Comment describing the method"
| tempVars | "Optional temp variables"
statements. "Period separates statements"
^ returnValue "Caret (^) denotes return"
square: x
"Returns the square of x"
^ x * x
6. Blocks (Anonymous Functions)
Blocks are enclosed in [ ] and can take arguments with :arg |.
[ "statements" ] "Zero-argument block"
[ :x | x + 1 ] "Single-argument block"
[ :x :y | x + y ] "Two-argument block"
value (no args)
- value: arg (one arg)
- value: arg1 value: arg2 (two args), etc.
7. Control Structures
Control structures are implemented via message passing to blocks. - Conditionals:
x > 0 ifTrue: [ 'positive' ] ifFalse: [ 'non-positive' ]
1 to: 10 do: [ :i | Transcript show: i ]
[ condition ] whileTrue: [ body ]
8. Literals
- Numbers:
42,3.14,16rFF(hex) - Characters:
$A(uppercase A) - Strings:
'hello'(single quotes) - Symbols:
#hello(interned string) - Arrays:
#(1 2 3)(static array) - Dynamic Arrays:
{1. 2. 1 + 2}(evaluated at runtime)
9. Class Definition
Classes are defined using a special syntax (usually in the IDE browser):
Object subclass: #MyClass
instanceVariableNames: 'var1 var2'
classVariableNames: 'ClassVar1'
poolDictionaries: ''
category: 'MyCategory'
1. Getters and Setters
Since Smalltalk enforces encapsulation, instance variables are private by default. To access them, you define accessor methods (getters/setters).
Example Class Definition
Object subclass: #Person
instanceVariableNames: 'name age'
classVariableNames: ''
poolDictionaries: ''
category: 'Examples'.
(a) Getters (Accessors)
Simply return the instance variable.
"Getter for 'name'"
name
^ name "Return the 'name' instance variable"
"Getter for 'age'"
age
^ age
(b) Setters (Mutators)
By convention, setters end with : and take an argument.
"Setter for 'name'"
name: newName
name := newName
"Setter for 'age'"
age: newAge
age := newAge
Usage
| p |
p := Person new.
p name: 'Alice'. "Setter"
p age: 30. "Setter"
Transcript show: p name; cr. "Getter → 'Alice'"
2. Constructors (Initialization)
Smalltalk doesn’t have constructors like Java/C++. Instead, you define instance initialization methods, typically:
- initialize (default setup)
- Class-side factory methods (e.g., newWithName:age:)
(a) The initialize Method
Called automatically after new if defined.
initialize
name := 'Untitled'.
age := 0.
(b) Custom Factory Methods (Class Methods)
Define these on the class side (not the instance side).
Step 1: Define the Method
"Class method: create a Person with given name and age"
Person class >> newWithName: aName age: anAge
| person |
person := self new. "Creates a new instance"
person name: aName. "Uses the setter"
person age: anAge.
^ person
Step 2: Usage
| p |
p := Person newWithName: 'Bob' age: 25.
Transcript show: p name; cr. "→ 'Bob'"
3. Complete Example
Class Definition
Object subclass: #Person
instanceVariableNames: 'name age'
classVariableNames: ''
poolDictionaries: ''
category: 'Examples'.
Instance Methods (Getters/Setters)
"Getters"
name
^ name
age
^ age
"Setters"
name: newName
name := newName
age: newAge
age := newAge
"Initialization"
initialize
name := 'Untitled'.
age := 0.
Class Methods (Factory)
"Class method for custom construction"
newWithName: aName age: anAge
| person |
person := self new.
person name: aName.
person age: anAge.
^ person
Usage
"Default construction"
p1 := Person new. "Uses initialize"
p1 name. "→ 'Untitled'"
"Custom construction"
p2 := Person newWithName: 'Alice' age: 30.
p2 name. "→ 'Alice'"
10. Cascading (Sending Multiple Messages)
Use ; to send multiple messages to the same receiver.
Transcript
show: 'Hello';
cr; "New line"
show: 'World'.
Summary of Key Features
- Everything is an object (including numbers and classes).
- No operators—just message passing.
- No explicit control structures—they are implemented via messages to blocks.
- Minimal syntax—only a few special characters (
:=,^,[],:).