3/8/20081CSE 3302 Programming LanguagesSmalltalkChengkai LiSpring 2008SmalltalkLecture 14 – Smalltalk, Spring 20081CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2008Everything is object. Objects yg j jcommunicate by messages.Lecture 14 – Smalltalk, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 20082Object HierarchyObjectUndefinedObject BooleanMagnitudeCollectionLecture 14 – Smalltalk, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 20083FractionFalseCharNumber…TrueInteger FloatSet…No Data Type.ypThere is only Class.Lecture 14 – Smalltalk, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 20084S llt lk S t i Si lSmalltalk Syntax is Simple.Lecture 14 – Smalltalk, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 20085Syntax• Smalltalk is really “small”– Only 6 keywords (pseudo variables)– Class, object, variable, method names are self explanatoryexplanatory – Only syntax for calling method (messages) and defining method. • No syntax for control structure• No syntax for creating classLecture 14 – Smalltalk, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 200863/8/20082Expressions• Literals• Pseudo Variables• Variables• Assignments• Blocks• MessagesLecture 14 – Smalltalk, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 20087Literals• Number: 3 3.5• Character: $a• String: ‘ ’ (‘Hel’,’lo!’ and ‘Hello!’ are two objects)• Symbol: # (#foo and #foo are the same object)• Compile-time (literal) array: #(1 $a 1+2)• Run-time (dynamic) array: {1. $a. 1+2}• Comment: “This is a comment.”Lecture 14 – Smalltalk, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 20088Pseudo Variables• true: singleton instance of True• false: singleton instance of False• nil: singleton instance of UndefinedObject•self:the object itself•self: the object itself• super: the object itself (but using the selector defined for the superclass)• thisContext: activation of method. (inspect the state of system)Lecture 14 – Smalltalk, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 20089Variables• Instance variables.• Local variables (method, blocks)| sampleCell width height n | • Arguments (method argument, block argument)– method argument:SBEGame»toggleNeighboursOfCellAt: i at: j– block argument: [ :i :j | self newCellAt: i at: j ]• Shared Variables:– Global variables, e.g., Transcript– Class variables, e.g., Epsilon in FloatLecture 14 – Smalltalk, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 200810Conventions• Class name, class variable, global variable:(Capital letter for the first character of every word)TableHashTable•Local variables arguments instance variable:Local variables, arguments, instance variable:(Capital letter for the first character of every word, except the first word)sampleCell• Object (instance of a class, especially arguments)aTableaHashTableLecture 14 – Smalltalk, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 200811Assignments• bounds := 0@0 corner: 16@16or• bounds _ 0@0 corner: 16@16• Assignment returns value, which is the object to the left of :=.Lecture 14 – Smalltalk, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2008123/8/20083Defining a Methodselector (method name)| local variable |statement (expression). (. is used to end a statement)statement(expression).^ return-value (^ returns value from a method)Lecture 14 – Smalltalk, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 200813Example of a method• FloatArray>>= aFloatArrayLecture 14 – Smalltalk, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 200814Methods and Messages• Method Name: Selector• Method Invocation: Message– Unary selector3 factorialobjectselectormessageobject selector (raiseTo:)message– Keyword selector3 raiseTo: 2‘Programming Language’ indexOf: $a startingAt: 3Lecture 14 – Smalltalk, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 200815objectselectorobjectselector ( indexOf:startingAt: )messageKeyword Selector: more readable• table insert: anItem at: anIndextable insert: 3 at: 5vs.• table.insert(anItem, anIndex)table.insert(3,5)Lecture 14 – Smalltalk, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 200816object selectorparameterBinary selector• 2 + 3• 2 + 3 + 4 ?• aTable / 3 (what it means depends on the class)• 1+2*3 ( * does not have higher precedence than -, because they th t b t t bj t N th ti l i iare messages that can be sent to any object. No mathematical meaning is assumed.)• Examples:– Integer>>#+– Complex>>#+– Fraction>>#+3/5(1/3) + (1/2)Lecture 14 – Smalltalk, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 200817Binary selector• + - * /• = (equality) ~= >= <= > <• == (identity, the two objects are the same object), ~~• & | Boolean•(string concatenation), (string concatenation)‘Hel’,’lo’ = ‘Hello’ ‘Hel’,’lo’ == ‘Hello’#Hello == #Hello• Assignment := is not a methodLecture 14 – Smalltalk, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2008183/8/20084Expression• Associativity for unary selector : left to right3 factorial isPrime• Associativity for binary selector : left to right1+2/4• Precedence rules:Unary selector, then Binary selector, then Keyword selectorUnary selector, then Binary selector, then Keyword selector2 raisedTo: 1 + 3 factorial• ( ) for changing the order of evaluation• “-object” was not there originally. So “3 - - 4" generated syntax errors in previous versions.Lecture 14 – Smalltalk, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 200819Message Cascading• i.e., Sequence OperatorTranscript cr.Transcript show: 'hello world'.Transcript crTranscript cr; show: 'hello world‘; crLecture 14 – Smalltalk, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 200820Abl ki f tiA block is an anonymous function.Lecture 14 – Smalltalk, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 200821Block• Evaluate a block: valueThe evaluation
View Full Document