CSc 520Principles of Programming Languages48: OO Languages — SmallTalkChristian CollbergDepartment of Computer ScienceUniversity of [email protected] 2005 Christian CollbergApril 22, 20051 History• During the 70’s Alan Kay worked on the Dynabook at Xerox Parc. A lot like today’s laptops, withintegrated touch screen, sound, networking.• Users would need some sort of programming skills to fully utilize the system. A language for non-experts was needed.• Smalltalk borrows from Simula, Logo (a language for children), and Sketchpad (a constraint-basedinteractive drawing system).• Smalltalk was the first “pure” object-oriented language. Every interaction is through sending a messageto an object.2 Running Smalltalk• On lectura, do the following:> cp /usr/local/lib/squeak/3.2-5/Squeak3.2-4956.* .> setenv SQUEAK_IMAGE $PWD/Squeak3.2-4956.image> /usr/local/lib/squeak/3.2-5/squeak3• Squeak’s start screen:14• Get rid of the crud:5• Open the class browser:26• Workspace lets you enter commands interactively. Transcript is “standard output.” do it executeshighlighted code.7• Create a new category cc.38• Create a new class MyClass. Select accept to add it.9• Click on no messages to get a message template.410• Create a method square. Select accept to add it.11• Execute square.512 Syntax• Square brackets [...] contain code.• Global items (variables, classes) begin with a capital letter. Other items start with lowercase.• Temporary variables: | x y z |.• Assignment: ← or :=, or type as .• Return value: ↑, type as ^.• The dot (.) is the statement terminator.13 Syntax — Unary Messages• A message M is sent to an object (receiver) R using the syntaxR M• A unary message has the syntaxR MFor example:D ← Dictionary new.14 Syntax — Binary Messages• A binary message M to receiver R with argument A has the syntaxR M A• For example:8 + 9This sends the message + to the object 8 with the argument 9.615 Syntax — Keyword Messages• A keyword message M to receiver R with arguments A1, A2, A3, . . . has the syntaxR M1: A1M2: A2M3: A3...• For example:DeannaTroi kiss: cheek how: tenderlyThis sends the message kiss:how: to the object DeannaTroi with the arguments cheek and tenderly.In Java we would have written:DeannaTroi.kisshow(cheek,tenderly)16 Syntax — Order of Evaluation• Messages are executed from left to right.• Parentheses can be used to force a particular order of evaluation.• Expressions are executed in the order1. unary messages,2. binary messages,3. keyword messages• Binary messages are executed left to right.17 Syntax — Cascading messages• Often we want to send several messages M1, M2, . . . to the same receiver. We can use the syntaxR M1:A1.R M2:A2.R M3:A3.• Or, we can cascade the messages using a semicolon (;):R M1:A1; M2:A2; M3:A3...• For example:Transcript show:5; cr; show:9; cr.18 Syntax — Blocks• A block is similar to a lambda expression. It’s syntax is:[arguments | code]• Arguments are prefixed by a colon (:):[:x :y | ↑ x+y ]719• Collections: Dictionary.20• Collections: Bag. 3 timesRepeat (from class Integer) sends the value: message to the block argu-ment 3 times.21• expression timesRepeat block sends the value: message to block as long as expression is true.822• do: aBlock enumerates all the receivers elements.23• ifTrue:ifFalse evaluates one of its blocks depending on the value of the receiver.9242526 Readings and References• Squeak download: http://www.squeak.org/download/• Squeak documentation: http://www.squeak.org/documentation/index.html• Squeak manual: http://www.phaidros.com/DIGITALIS/englisch/sqk/sqk00002.htm• http://www.cosc.canterbury.ac.nz/~wolfgang/cosc205/smalltalk1.html#link• http://www.cosc.canterbury.ac.nz/~wolfgang/cosc205/labs/labs98.html• Read Scott: pp.
View Full Document