Smalltalk
- File size
- 5.5KB
- Lines of code
- 154
Smalltalk
Everything is an object in Smalltalk.
Introduction
- dynamically-typed
- fully object-oriented
- reflective programming
- used for educational purposes and constructionist learning
Quickstart
- every statement is . period-delimited
- there are NO function calls, instead, messages are sent to objects which then run a method in response which themselves return an object
- a basic operation is to send a message to an object in the format
<objectName> <desiredMessage> - there are three types of messages
- Unary: a single symbol comprised of several words in camelCase with no arguments
- here are some examples
- size
- reverseBytes
- convertToLargerFormatPixels
- here are some examples
- Binary: small set of reserved symbols often used for arithmetic operations in most other programming languages, that receive a single argument
- note there is no traditional arithmetic precendece in binary messages
- here are some examples
- 3 + 4
- 10 - 5
- 6 * 7
- 20 / 4
- 15 // 4
- 15 \ 4
- 5 = 5
- 5 ~= 4
- 5 < 10
- 10 > 5
- 5 <= 5
- 10 >= 5
- true & false
- true | false
- true not
- Keyword: general form in camelCase that receives multiple arguments which are : colon-delimited
- here are some examples
- setTemperature:
- at:put:
- drawFrom:to:lineWidth:fillColor:
- here are some examples
- Unary: a single symbol comprised of several words in camelCase with no arguments
" ----- QUICKSTART ----- "
" below is some annotated Smalltalk code "
" honestly if you're struggling to understand, don't worry because I am too "
" --- CLASS DEFINITION --- "
Object subclass: Person [ " define a new class Person which is a subclass of Object "
| name age | " define instance variables for the class "
Person class >> newName: aName age: anAge [ " class method to create a new Person instance with a name and age and return it "
^ self new setName: aName; setAge: anAge; yourself.
]
setName: aName [ " method to set the name of the person "
name := aName.
]
setAge: anAge [ " method to set the age of the person "
age := anAge.
]
printPerson [ " method to print the details of the person "
Transcript show: 'Name: ', name; cr.
Transcript show: 'Age: ', age printString; cr.
]
]
" --- EXECUTION CODE --- "
| person |
person := Person newName: 'Alice' age: 30. " create an instance of the Person class with the name 'Alice' and age 30 "
person printPerson. " print the details of the person instance by calling the printPerson method "