CSE 3302 Programming Languages Smalltalk Chengkai Li Spring

  • Slides: 28
Download presentation
CSE 3302 Programming Languages Smalltalk Chengkai Li Spring 2008 Lecture 14 – Smalltalk, Spring

CSE 3302 Programming Languages Smalltalk Chengkai Li Spring 2008 Lecture 14 – Smalltalk, Spring 2008 CSE 3302 Programming Languages, UT-Arlington ©Chengkai Li, 2008 1

Everything is object. Objects communicate by messages. Lecture 14 – Smalltalk, Spring 2008 CSE

Everything is object. Objects communicate by messages. Lecture 14 – Smalltalk, Spring 2008 CSE 3302 Programming Languages, UT-Arlington ©Chengkai Li, 2008 2

Object Hierarchy Object Undefined. Object Boolean True Magnitude False Char Fraction Lecture 14 –

Object Hierarchy Object Undefined. Object Boolean True Magnitude False Char Fraction Lecture 14 – Smalltalk, Spring 2008 Collection Set … Number … Integer CSE 3302 Programming Languages, UT-Arlington ©Chengkai Li, 2008 Float 3

No Data Type. There is only Class. Lecture 14 – Smalltalk, Spring 2008 CSE

No Data Type. There is only Class. Lecture 14 – Smalltalk, Spring 2008 CSE 3302 Programming Languages, UT-Arlington ©Chengkai Li, 2008 4

Smalltalk Syntax is Simple. Lecture 14 – Smalltalk, Spring 2008 CSE 3302 Programming Languages,

Smalltalk Syntax is Simple. Lecture 14 – Smalltalk, Spring 2008 CSE 3302 Programming Languages, UT-Arlington ©Chengkai Li, 2008 5

Syntax • Smalltalk is really “small” – Only 6 keywords (pseudo variables) – Class,

Syntax • Smalltalk is really “small” – Only 6 keywords (pseudo variables) – Class, object, variable, method names are self explanatory – Only syntax for calling method (messages) and defining method. • No syntax for control structure • No syntax for creating class Lecture 14 – Smalltalk, Spring 2008 CSE 3302 Programming Languages, UT-Arlington ©Chengkai Li, 2008 6

Expressions • • • Literals Pseudo Variables Assignments Blocks Messages Lecture 14 – Smalltalk,

Expressions • • • Literals Pseudo Variables Assignments Blocks Messages Lecture 14 – Smalltalk, Spring 2008 CSE 3302 Programming Languages, UT-Arlington ©Chengkai Li, 2008 7

Literals • • Number: 3 3. 5 Character: $a String: ‘ ’ (‘Hel’, ’lo!’

Literals • • 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 2008 CSE 3302 Programming Languages, UT-Arlington ©Chengkai Li, 2008 8

Pseudo Variables true: singleton instance of True false: singleton instance of False nil: singleton

Pseudo Variables true: singleton instance of True false: singleton instance of False nil: singleton instance of Undefined. Object self: the object itself super: the object itself (but using the selector defined for the superclass) • this. Context: activation of method. (inspect the state of system) • • • Lecture 14 – Smalltalk, Spring 2008 CSE 3302 Programming Languages, UT-Arlington ©Chengkai Li, 2008 9

Variables • Instance variables. • Local variables (method, blocks) | sample. Cell width height

Variables • Instance variables. • Local variables (method, blocks) | sample. Cell width height n | • Arguments (method argument, block argument) – method argument: SBEGame» toggle. Neighbours. Of. Cell. At: i at: j – block argument: [ : i : j | self new. Cell. At: i at: j ] • Shared Variables: – Global variables, e. g. , Transcript – Class variables, e. g. , Epsilon in Float Lecture 14 – Smalltalk, Spring 2008 CSE 3302 Programming Languages, UT-Arlington ©Chengkai Li, 2008 10

Conventions • Class name, class variable, global variable: (Capital letter for the first character

Conventions • Class name, class variable, global variable: (Capital letter for the first character of every word) Table Hash. Table • Local variables, arguments, instance variable: (Capital letter for the first character of every word, except the first word) sample. Cell • Object (instance of a class, especially arguments) a. Table a. Hash. Table Lecture 14 – Smalltalk, Spring 2008 CSE 3302 Programming Languages, UT-Arlington ©Chengkai Li, 2008 11

Assignments • bounds : = 0@0 corner: 16@16 or • bounds _ 0@0 corner:

Assignments • bounds : = 0@0 corner: 16@16 or • bounds _ 0@0 corner: 16@16 • Assignment returns value, which is the object to the left of : =. Lecture 14 – Smalltalk, Spring 2008 CSE 3302 Programming Languages, UT-Arlington ©Chengkai Li, 2008 12

Defining a Method selector (method name) | local variable | statement (expression). (. is

Defining a Method selector (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 2008 CSE 3302 Programming Languages, UT-Arlington ©Chengkai Li, 2008 13

Example of a method • Float. Array>>= a. Float. Array Lecture 14 – Smalltalk,

Example of a method • Float. Array>>= a. Float. Array Lecture 14 – Smalltalk, Spring 2008 CSE 3302 Programming Languages, UT-Arlington ©Chengkai Li, 2008 14

Methods and Messages • Method Name: Selector • Method Invocation: Message – Unary selector

Methods and Messages • Method Name: Selector • Method Invocation: Message – Unary selector message 3 factorial object selector – Keyword selector 3 raise. To: 2 object message selector (raise. To: ) message ‘Programming Language’ index. Of: $a starting. At: 3 object Lecture 14 – Smalltalk, Spring 2008 selector ( index. Of: starting. At: ) CSE 3302 Programming Languages, UT-Arlington ©Chengkai Li, 2008 15

Keyword Selector: more readable • table insert: an. Item at: an. Index table insert:

Keyword Selector: more readable • table insert: an. Item at: an. Index table insert: 3 at: 5 vs. • table. insert(an. Item, an. Index) table. insert(3, 5) Lecture 14 – Smalltalk, Spring 2008 CSE 3302 Programming Languages, UT-Arlington ©Chengkai Li, 2008 16

Binary selector • 2 + object 3 selector • 2 + 3 + 4

Binary selector • 2 + object 3 selector • 2 + 3 + 4 parameter ? • a. Table / 3 (what it means depends on the class) • 1+2*3 ( * does not have higher precedence than -, because they are 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 2008 CSE 3302 Programming Languages, UT-Arlington ©Chengkai Li, 2008 17

Binary selector • • • + - * / = (equality) ~= >= <=

Binary selector • • • + - * / = (equality) ~= >= <= > < == (identity, the two objects are the same object), ~~ & | Boolean , (string concatenation) ‘Hel’, ’lo’ = ‘Hello’ ‘Hel’, ’lo’ == ‘Hello’ #Hello == #Hello • Assignment : = is not a method Lecture 14 – Smalltalk, Spring 2008 CSE 3302 Programming Languages, UT-Arlington ©Chengkai Li, 2008 18

Expression • Associativity for unary selector : left to right 3 factorial is. Prime

Expression • Associativity for unary selector : left to right 3 factorial is. Prime • Associativity for binary selector : left to right 1+2/4 • Precedence rules: Unary selector, then Binary selector, then Keyword selector 2 raised. To: 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 2008 CSE 3302 Programming Languages, UT-Arlington ©Chengkai Li, 2008 19

Message Cascading • i. e. , Sequence Operator Transcript cr. Transcript show: 'hello world'.

Message Cascading • i. e. , Sequence Operator Transcript cr. Transcript show: 'hello world'. Transcript cr; show: 'hello world‘; cr Lecture 14 – Smalltalk, Spring 2008 CSE 3302 Programming Languages, UT-Arlington ©Chengkai Li, 2008 20

A block is an anonymous function. Lecture 14 – Smalltalk, Spring 2008 CSE 3302

A block is an anonymous function. Lecture 14 – Smalltalk, Spring 2008 CSE 3302 Programming Languages, UT-Arlington ©Chengkai Li, 2008 21

Block • Evaluate a block: value The evaluation result is the object from the

Block • Evaluate a block: value The evaluation result is the object from the last statement. [ 1+2 ] value [ 1+2. ‘abc’, ‘def’] value [ 1+2. SBEGame new] value Lecture 14 – Smalltalk, Spring 2008 CSE 3302 Programming Languages, UT-Arlington ©Chengkai Li, 2008 22

Block Parameters • [: x : y | x+y ] value: 2 value: 3

Block Parameters • [: x : y | x+y ] value: 2 value: 3 • [ : x : y | | z : = x + y. z : = z * z. z ] value: 2 value: 3 Lecture 14 – Smalltalk, Spring 2008 CSE 3302 Programming Languages, UT-Arlington ©Chengkai Li, 2008 23

Block Closure • Block can access variables declared in enclosing scope. | x [

Block Closure • Block can access variables declared in enclosing scope. | x [ [ x | : = 1. : y | x + y ] value: 2. : y | self x + y ] value: 2. Lecture 14 – Smalltalk, Spring 2008 CSE 3302 Programming Languages, UT-Arlington ©Chengkai Li, 2008 24

Block is Object! z : = [: x : y | x+y ]. z

Block is Object! z : = [: x : y | x+y ]. z value: 2 value: 3 Lecture 14 – Smalltalk, Spring 2008 CSE 3302 Programming Languages, UT-Arlington ©Chengkai Li, 2008 25

“Control Structures” by Messages • Conditions: Messages to Boolean objects, with blocks as arguments

“Control Structures” by Messages • Conditions: Messages to Boolean objects, with blocks as arguments class True (subclass of Boolean, False is similar) Selectors: – if. True: alternative. Block ^ alternative. Block value – if. False: alternative. Block ^nil – if. True: if. False: – if. False: if. True: • Example – (a < b) if. True: [max: =b] if. False: [max: =a] Lecture 14 – Smalltalk, Spring 2008 CSE 3302 Programming Languages, UT-Arlington ©Chengkai Li, 2008 26

“Control Structures” by Messages • While Loops : blocks as message receivers • Example

“Control Structures” by Messages • While Loops : blocks as message receivers • Example – n : = 1. [ n < 10 ] while. True: [ n : = n*2 ] Lecture 14 – Smalltalk, Spring 2008 CSE 3302 Programming Languages, UT-Arlington ©Chengkai Li, 2008 27

“Control Structures” by Messages • Counting Loops : blocks as parameters • Example –

“Control Structures” by Messages • Counting Loops : blocks as parameters • Example – n : = 1. 10 times. Repeat: [ n : = n*2 ] – n : = 1. 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 2008 CSE 3302 Programming Languages, UT-Arlington ©Chengkai Li, 2008 28