Hotspots: Admin Pages | Turn-in Site |
Current Links: Cases Final Project Summer 2007
A Smalltalk Quick Reference Guide
This page is simply a collection of tips and reminders to help you get started with Smalltalk.
Objects, Messages and Methods
- Everything in Smalltalk is an object.
- There are no primitive types in Smalltalk; 4 is just a shorter way of describing an instance of a SmallInteger object that represents the number 4.
- The only way to interact with objects is by sending them messages. Anything written to the right of an object is a message; for example, in the code '4 squared', 4 is the object and squared is the message passed to the object.
- In Smalltalk, methods are defined as the messages that an object will respond to. For example, in the code "myString squared" (where myString is, in fact, a String), squared is a valid message (it's already been defined by SmallInteger); however, because squared is not a method of String (you can't square a letter), no action will be taken by myString.
Symbols In Smalltalk
- The equals symbol ( = ) is used to determine the equality of two boolean statements, and is not used to assign a value to a variable as is the case in Java and C. This replaces the == symbol in Java and C.
- The set symbol ( := ) is used to assign values to variables. It replaces the = symbol in Java and C.
- The single quote symbol ( ' ' ) is used to represent Strings. This replaces the "" symbol in Java and C.
- The double quote symbol ( " " ) is used to denote comments. This replaces the various comment symbols used in Java and C.
- The comma symbol ( , ) is used to concatenate two Strings in Smalltalk. This replaces the plus sign used in Java to automatically concatenate Strings.
- The period symbol ( . ) is used to end a statement in Smalltalk. This replaces the ; symbol in Java and C.
- A unary message is a message that only requires a method name to be passed to an object. "4 squared", "'string' reverse", and "6.9 rounded" are all unary messages, as the object that contains the method only needs the name of the method to function properly.
- A binary message is a message that requires both the method name and an object to be passed to the first object. "4 + 6", "'cat', 'nip'", and "6.9 - 17.2" are all binary messages, as they require a method and an object to be passed to another object to function correctly.
- A keyword message is a message that indicates that additional parameters are going to be passed into the object, and is denoted by a colon ( : ). In the code "Transcript show: 6 + 7 + 9", the Transcript class is passed the show: method with the parameter "6 + 7 + 9" and displays the result (22) on the console. In the code "('Got Milk?') copy replaceFrom: 5 to: 8 with: 'I am lost' startingAt: 6", however, the method name takes four separate parameters and is named "replaceFrom:to:with:startingAt:". It's confusing, but it makes sense if you think of a series of keywords like the one listed previously as different segments of a single method.
- The order of operations in Smalltalk is as follows:
As you can see, order of operations ignores mathematical conventions. If you type "x := 1 + 2 * 3", you will see that the value of x will be 9 instead of 7. Be sure to use parentheses whenever you are unsure of the order of operations in Smalltalk.
- Left to right
- Parenthetical statements are resolved
- Unary messages
- Binary messages
- Keyword messages
- Class-side methods are methods called on static classes and don't depend on specific instances to execute. They function similarly to static methods in Java. "Transcript show:" is probably the most used Smalltalk example of a class-side method, although there are many others.
- Instance-side methods are methods that are called on instances of classes and depend on the unique information within the instance to function. These methods are similar to normal object methods in Java. "10 squared" and "9 + 1" are both examples of instance-side methods, as they depend on the values contained within the objects to function properly.
- Encapsulation is the ability of objects to contain other objects within them, allowing for large and powerful objects to easily exist without needing to represent everything as thousands of primitive types.
- Inheritance is the ability of a subclass to use all the methods of its parent classes. Unlike Java, multiple inheritances are allowed in Smalltalk, allowing for design patterns impossible to create in some other languages.
- Polymorphism is the ability of a class to be cast as a super- or sub-class to allow general algorithms to have widespread use.
- All dates and times in Smalltalk are GMT.
- "self halt" stops the application upon execution and opens up a debugger to allow you to step through lines of executable code one at a time.
- The Visualworks Store version control is the simplest way to maintain your application code as multiple people submit changes at once.
- Try to use a clean namespace whenever you load a new application, as a single name conflict could render an application unusable.
- You need a block of code to perform a loop (i.e. [a = b] whileFalse: [...]), while you only need a boolean value to execute an if-statement (i.e. booleanValue ifTrue: [...]).
- You can press Ctrl-F and Ctrl-T to instantly type ifFalse: and ifTrue:, respectively.
Links to this Page
- Cases last edited on 30 July 2011 at 2:33 am by r59h132.res.gatech.edu
- Index of Individual Cases last edited on 3 May 2011 at 12:46 pm by r52h48.res.gatech.edu