Smalltalk (and Squeak) Aida Dungan and Rick Shreve
Historical perspective:
Squeak! The primary design team is at Disney, but again, it is open-source.
What is Squeak?
What’s legal?
Data objects:
Example goes here:
Example:
Conditionals While A loop which functions as a while loop from C++ would be implemented in Smalltalk in the following way: i:=0. [(i<5)] whileTrue:[Transcript show: ' Number: '. Transcript show: (i). i:=i+1.]. “This code generates the following output:” Number: 0 Number: 1 Number: 2 Number: 3 Number: 4
If / Else a:=5. b:=2. ((a+b)<10) "this is the evaluated expression" ifTrue: [Transcript show: 'less than ten'; cr.] ifFalse: [Transcript show: 'greater than or equal to ten'; cr.]. “The above code produces the following: “ less than ten
If (continued) You can use a single ifTrue or ifElse with a preceding expression to be evaluated, and a following statement block, or you can use them together, as in the previous example, as long as you only put a period after the last block.
Some basic syntax There is no need to import libraries. The end of line command is a period. “This is a comment in Smalltalk. Use double quotes.” Variable declarations are done like this: |x y z| “no type specifications for variables.”
Some more basic syntax Smalltalk is interpreted, so there is no compilation procedure. To run your code, select all of the text, left-click, and choose ‘do it’ from the menu. Assignment is done either as x_1 or x:=1. Equality is checked with a single x=y (rather than == of C++). Brackets are the block delineators [code goes here] ^value is how you signify a return value.
Data objects:
The For Loop Similar to Fortran, the Smalltalk For loop is done with a to:by:do: statement. 1 to: 100 by: 20 do: [:x | Transcript show: ‘hello world ‘; cr.] “The above code produces the following: “ hello world
Fileout / filein The way to store your code as a small text file (without the Squeak image) if to create a *.st file using the fileout command. The directions for making the fileout are included in part of the assignment Dr. Bruce has assigned. To read this code into a Squeak image, you must open a file list, and use the filein command. This process is detailed at
Comments and Literals zComments “here is one comment that can span many many lines” zNumbers instances of class Number (Integers, Floats, Fractions) zCharacters (Instances of class Character) character preceded by a dollar sign $x $3 $< $$ zStrings (Instances of class String) ‘sequence of “with comment inside” characters’
Pseudo-variables are reserved identifiers that are similar to keywords in other languages. nil true false are constants. self - the current object, that is, the receiver of the current message; like ( this in C++) super - refers to the same object as self. However, the search for a suitable method starts in the superclass of this class. Pseudo - Variables
Message Expressions zThe association of a method with an object is called a message. zMessage expressions are sequences of messages sent to objects. zExpressions consist of selectors followed by their arguments if any. Messages names should indicate the type of action desired. zThere are three types of messages: Unary messages: (new abs factorial sqrt asString) Binary messages: (+n -n *n /n \\ -”remainder”) Keyword messages: (6 between: 3 and: 9 - “true”) more on messages Available :
Unary messages zMessages without arguments. Unary message to a class: ’new’ returns a newly created instance of a class MyFriend zMessages that are most tightly parsed left to right. “same as (((9 sqrt)rounded)asString) 9 sqrt rounded asString |friend| friend := MyFriend new
Binary Messages Used for arithmetic operations Binary messages are always parsed left to right, regardless of the precedence of numeric operations. Use of parentheses is a good idea!!!!!! 3+4 77 3+4*5 factorial 27
Keyword messages Use keywords to name arguments. stores value in array at index same, but stores at index factorial Messages can be nested. Parsing order is left to right, precedence is: unary binary keyword, use parentheses if necessary. Messages sent to the same receiver may be “cascaded” just use semicolons; array at: 3 put: 12 array at: 3factorial put 12 receiver show; +23; at:23 put: 4
Example in Squeak Windows
Block Expressions zBlock without arguments [expressionSequence] zBlock with arguments and local variables. [ (:identifier)+||identifier+|expressionSequence] block that an argument global message new is an object variable line name 1 to: 5 do: [:I | (Transcript show: ‘Hello friend’) cr]
It’s true us long us it’s not false ifTrue: alternativeBlock ifFalse: alternativeBlock. They can be used separately or together to form if/else block. If used together, put a period after the second block, but not the first.
Inheritance zObject is the root of Smalltalk’s inheritance hierarchy, providing some basic functionality to all objects. zSmalltalk only supports single inheritance, but methods can be passed through multiple levels of parent classes. zIf any method is not defined for an object, the parent of that object is called with the same method.
Inheritance Object Magnitude Number Float Integer Fraction Character Boolean False True
Building an application