Smalltalk Smalltalk
Total Page:16
File Type:pdf, Size:1020Kb
3/8/2008 CSE 3302 Programming Languages Smalltalk Everyygthing is obj ect. Obj ects communicate by messages. Chengkai Li Spring 2008 Lecture 14 – Smalltalk, Spring Lecture 14 – Smalltalk, Spring CSE3302 Programming Languages, UT-Arlington 1 CSE3302 Programming Languages, UT-Arlington 2 2008 ©Chengkai Li, 2008 2008 ©Chengkai Li, 2008 Object Hierarchy Object UndefinedObject Boolean Magnitude Collection No Data Type. True False Set … There is only Class. Char Number … Fraction Integer Float Lecture 14 – Smalltalk, Spring Lecture 14 – Smalltalk, Spring CSE3302 Programming Languages, UT-Arlington 3 CSE3302 Programming Languages, UT-Arlington 4 2008 ©Chengkai Li, 2008 2008 ©Chengkai Li, 2008 Syntax • Smalltalk is really “small” – Only 6 keywords (pseudo variables) – Class, object, variable, method names are self explanatory Sma llta lk Syn tax is Simp le. – Only syntax for calling method (messages) and defining method. • No syntax for control structure • No syntax for creating class Lecture 14 – Smalltalk, Spring Lecture 14 – Smalltalk, Spring CSE3302 Programming Languages, UT-Arlington 5 CSE3302 Programming Languages, UT-Arlington 6 2008 ©Chengkai Li, 2008 2008 ©Chengkai Li, 2008 1 3/8/2008 Expressions Literals • Literals • Number: 3 3.5 • Character: $a • Pseudo Variables • String: ‘ ’ (‘Hel’,’lo!’ and ‘Hello!’ are two objects) • Symbol: # (#foo and #foo are the same object) • Variables • Compile-time (literal) array: #(1 $a 1+2) • Assignments • Run-time (dynamic) array: {1. $a. 1+2} • Comment: “This is a comment.” • Blocks • Messages Lecture 14 – Smalltalk, Spring Lecture 14 – Smalltalk, Spring CSE3302 Programming Languages, UT-Arlington 7 CSE3302 Programming Languages, UT-Arlington 8 2008 ©Chengkai Li, 2008 2008 ©Chengkai Li, 2008 Pseudo Variables Variables • Instance variables. • true: singleton instance of True • Local variables (method, blocks) • false: singleton instance of False | sampleCell width height n | • nil: singleton instance of UndefinedObject • Arguments (method argument, block argument) • self: the object itself – method argument: • super: the object itself (but using the selector SBEGame»toggleNeighboursOfCellAt: i at: j – block argument: defined for the superclass) [ :i :j | self newCellAt: i at: j ] • thisContext: activation of method. (inspect the state of system) • Shared Variables: – Global variables, e.g., Transcript – Class variables, e.g., Epsilon in Float Lecture 14 – Smalltalk, Spring Lecture 14 – Smalltalk, Spring CSE3302 Programming Languages, UT-Arlington 9 CSE3302 Programming Languages, UT-Arlington 10 2008 ©Chengkai Li, 2008 2008 ©Chengkai Li, 2008 Conventions Assignments • Class name, class variable, global variable: (Capital letter for the first character of every word) • bounds := 0@0 corner: 16@16 Table HashTable or • bounds _ 0@0 corner: 16@16 • Local variables , arguments, instance variable: (Capital letter for the first character of every word, except the first word) sampleCell • Assignment returns value, which is the object • Object (instance of a class, especially arguments) to the left of :=. aTable aHashTable Lecture 14 – Smalltalk, Spring Lecture 14 – Smalltalk, Spring CSE3302 Programming Languages, UT-Arlington 11 CSE3302 Programming Languages, UT-Arlington 12 2008 ©Chengkai Li, 2008 2008 ©Chengkai Li, 2008 2 3/8/2008 Defining a Method Example of a method selector (method name) • FloatArray>>= aFloatArray | local variable | statement (expression). (. is used to end a statement) statement(expression). ^ return-value (^ returns value from a method) Lecture 14 – Smalltalk, Spring Lecture 14 – Smalltalk, Spring CSE3302 Programming Languages, UT-Arlington 13 CSE3302 Programming Languages, UT-Arlington 14 2008 ©Chengkai Li, 2008 2008 ©Chengkai Li, 2008 Methods and Messages Keyword Selector: more readable • Method Name: Selector • Method Invocation: Message • table insert: anItem at: anIndex – Unary selector 3 factorial message table insert: 3 at: 5 object selector vs. – Keyword selector message • table.insert(anItem, anIndex) 3 raiseTo: 2 object selector (raiseTo:) message table.insert(3,5) ‘Programming Language’ indexOf: $a startingAt: 3 object selector ( indexOf:startingAt: ) Lecture 14 – Smalltalk, Spring Lecture 14 – Smalltalk, Spring CSE3302 Programming Languages, UT-Arlington 15 CSE3302 Programming Languages, UT-Arlington 16 2008 ©Chengkai Li, 2008 2008 ©Chengkai Li, 2008 Binary selector Binary selector • 2 + 3 parameter object selector • + - * / • 2 + 3 + 4 ? • = (equality) ~= >= <= > < • • aTable / 3 (what it means depends on the class) == (identity, the two objects are the same object), ~~ • • 1+2*3 ( * does not have higher precedence than -, because they & | Boolean are messages tha t can be sen t to any o bjec t. No ma thema tica l mean ing is • , (string concatenation) assumed.) • Examples: ‘Hel’,’lo’ = ‘Hello’ – Integer>>#+ ‘Hel’,’lo’ == ‘Hello’ – Complex>>#+ #Hello == #Hello – Fraction>>#+ 3/5 • Assignment := is not a method (1/3) + (1/2) Lecture 14 – Smalltalk, Spring Lecture 14 – Smalltalk, Spring CSE3302 Programming Languages, UT-Arlington 17 CSE3302 Programming Languages, UT-Arlington 18 2008 ©Chengkai Li, 2008 2008 ©Chengkai Li, 2008 3 3/8/2008 Expression Message Cascading • Associativity for unary selector : left to right 3 factorial isPrime • • Associativity for binary selector : left to right i.e., Sequence Operator 1+2/4 Transcript cr. • Precedence rules: Transcript show: 'hello world'. Unary selector, then Binary selector, then Keyword selector Transcript cr 2 raisedTo: 1 + 3 factorial Transcript cr; show: 'hello world‘; cr • ( ) for changing the order of evaluation • “-object” was not there originally. So “3 - - 4" generated syntax errors in previous versions. Lecture 14 – Smalltalk, Spring Lecture 14 – Smalltalk, Spring CSE3302 Programming Languages, UT-Arlington 19 CSE3302 Programming Languages, UT-Arlington 20 2008 ©Chengkai Li, 2008 2008 ©Chengkai Li, 2008 Block • Evaluate a block: value The evaluation result is the object from the last statement. AblA bloc kik is an anonymous func tion. [ 1+2 ] value [ 1+2.‘abc’, ‘def’] value [ 1+2. SBEGame new] value Lecture 14 – Smalltalk, Spring Lecture 14 – Smalltalk, Spring CSE3302 Programming Languages, UT-Arlington 21 CSE3302 Programming Languages, UT-Arlington 22 2008 ©Chengkai Li, 2008 2008 ©Chengkai Li, 2008 Block Parameters Block Closure • [:x :y | x+y ] value:2 value:3 • Block can access variables declared in • [ :x :y | | z | enclosing scope. z := x + y. z := z * z. ||| x | z x := 1. ] value: 2 value: 3 [ :y | x + y ] value: 2. [ :y | self x + y ] value: 2. Lecture 14 – Smalltalk, Spring Lecture 14 – Smalltalk, Spring CSE3302 Programming Languages, UT-Arlington 23 CSE3302 Programming Languages, UT-Arlington 24 2008 ©Chengkai Li, 2008 2008 ©Chengkai Li, 2008 4 3/8/2008 Block is Object! “Control Structures” by Messages • Conditions: Messages to Boolean objects, with z := [:x :y | x+y ]. blocks as arguments z value:2 value:3 class True (subclass of Boolean, False is similar) Selectors: – ifTrue: alternativeBlock ^ alternativeBlock value – ifFalse: alternativeBlock ^nil – ifTrue:ifFalse: – ifFalse:ifTrue: • Example – (a < b) ifTrue: [max:=b] ifFalse: [max:=a] Lecture 14 – Smalltalk, Spring Lecture 14 – Smalltalk, Spring CSE3302 Programming Languages, UT-Arlington 25 CSE3302 Programming Languages, UT-Arlington 26 2008 ©Chengkai Li, 2008 2008 ©Chengkai Li, 2008 “Control Structures” by Messages “Control Structures” by Messages • Counting Loops : blocks as parameters • While Loops : blocks as message receivers • Example • Example – n := 1. – n := 1. 10 timesRepeat: [ n := n*2 ] – n := 1. [ n < 10 ] whileTrue: [ n : = nn2*2] ] 1 to: 10 do: [ n := n*2 ] – n := 0. 1 to: 10 do: [ :i | n := n + i ] – n := 0. 1 to: 10 by: 2 do: [ :i | n := n + i ] – n := 0. 10 to: 1 by: -2 do: [ :i | n := n + i ] • Let’s see how Number>>to:do: is implemented Lecture 14 – Smalltalk, Spring Lecture 14 – Smalltalk, Spring CSE3302 Programming Languages, UT-Arlington 27 CSE3302 Programming Languages, UT-Arlington 28 2008 ©Chengkai Li, 2008 2008 ©Chengkai Li, 2008 5.