Title: Smalltalk%20(and%20Squeak)
1Smalltalk (and Squeak)
Aida Dungan and Rick Shreve
2Historical perspective
3(No Transcript)
4(No Transcript)
5(No Transcript)
6Squeak!
The primary design team is at Disney, but again,
it is open-source.
7What is Squeak?
8Whats legal?
9Data objects
10Example goes here
11Example
12Conditionals
While A loop which functions as a while loop from
C would be implemented in Smalltalk in the
following way i0. (ilt5) whileTrueTranscrip
t show ' Number '. Transcript
show (i). ii1..
This code generates the following output
Number 0 Number 1 Number 2 Number 3 Number 4
13If / Else
a5. b2. ((ab)lt10) "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
14If (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.
15Some 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.
16Some 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 x1. Equality is checked with a single xy
(rather than of C). Brackets are the block
delineators code goes here value is how you
signify a return value.
17Data objects
18The For Loop
Similar to Fortran, the Smalltalk For loop is
done with a tobydo statement. 1 to 100 by
20 do x Transcript show hello world cr.
The above code produces the following
hello world hello world hello world hello
world hello world
19Fileout / 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 http//www.cs.unca.edu/bru
ce/Fall00/csci431/lecture13/smalltalkfilelist.htm.
20Comments and Literals
- Comments
- here is one comment that can span many
many lines - Numbers
- instances of class Number (Integers,
Floats, Fractions) - Characters (Instances of class Character)
- character preceded by a dollar sign
- x 3 lt
- Strings (Instances of class String)
- sequence of with comment inside
characters -
-
21Pseudo - Variables
- 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.
22Message Expressions
- The association of a method with an object is
called a message. - Message expressions are sequences of messages
sent to objects. - Expressions consist of selectors followed by
their arguments if any. - Messages names should indicate the type of action
desired. - There 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 http//mucow.com/SqueakClassesRef.html
23Unary messages
- Messages without arguments.
- Unary message to a class new returns a
newly created instance of a class MyFriend - Messages that are most tightly parsed left to
right. -
- same as (((9 sqrt)rounded)asString)
friend friend MyFriend new
9 sqrt rounded asString
24Binary 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!!!!!!
34
?7
345
?35
34factorial
?27
25Keyword 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 at23 put 4
26Example in Squeak Windows
27Block Expressions
- Block without arguments
-
expressionSequence - Block with arguments and local variables.
-
(identifier)identifierex
pressionSequence - block that an argument global
message new - is an object
variable line -
-
name 1 to 5 do I (Transcript show Hello
friend) cr
28Its true us long us its 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.
29Inheritance
- Object is the root of Smalltalks inheritance
hierarchy, providing some basic functionality to
all objects. - Smalltalk only supports single inheritance, but
methods can be passed through multiple levels of
parent classes. - If any method is not defined for an object, the
parent of that object is called with the same
method.
30Inheritance
- Object
-
- Magnitude
- Number
- Float
- Integer
- Fraction
- Character
- Boolean
- False
- True
31Building an application