1 / 17

Introduction to Smalltalk

Introduction to Smalltalk. Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 7.2.4 Appendix A.12. Smalltalk overview. Smalltalk was developed by Alan Kay at the Xerox Palo Alto Research Center (PARC) in the early 1970s.

vnecaise
Download Presentation

Introduction to Smalltalk

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Smalltalk Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 7.2.4 Appendix A.12

  2. Smalltalk overview Smalltalk was developed by Alan Kay at the Xerox Palo Alto Research Center (PARC) in the early 1970s. Goal was a total personal computing environment In 1972, Dan Ingalls developed the first implementation of Smalltalk-72, although Smalltalk-80 is the generally accepted definition of the language. Smalltalk has several features that make it unique among most languages: • A total environment design, not just a language • A minimal language design (comes with a predefined set of class definitions) • Execution model - The execution model for Smalltalk is based on a communication model. Data in Smalltalk consist of objects, and methods are considered to be messages sent to objects. • Smalltalk uses a dynamic execution sequencing model.

  3. Language models Look at language execution models again: Imperative - sequences of actions on data: C, C++, FORTRAN, FORTH, Postscript, Ada, Basic, Pascal Applicative - Develop functions that represent final transformation of data: ML, LISP Rule-based - Specify format of results - BNF, Prolog Message-based - A program is a network that dynamically sends messages to objects - Smalltalk

  4. Smalltalk features Smalltalk: 1. Each object is an instance of a class. 2. Each class has a set of associated methods (functions). 3. Execution proceeds by sending a message (which is one of these methods) to an object of that class. Historically Smalltalk was the first language to have these features. These were later borrowed for C++ and Java.

  5. Smalltalk example 1 Array variableSubclass: #Datastore 2 instanceVariableNames: '' 3 classVariableNames: 'DataFile ArrIndex Storage Size' 4 poolDictionaries: '' 5 category: nil ! 6 !Datastore class methodsFor: 'instance creation'! 7 new 8 DataFile _ FileStream open:'data' mode: 'r'. 9 Storage _ Array new: 99. 10 Size _ 99. 11 self reset !! 12 !Datastore class methodsFor: 'basic'! 13 asgn: aValue 14 ArrIndex _ ArrIndex + 1. 15 Storage at: ArrIndex put: aValue ! 16 getval 17 ArrIndex _ ArrIndex + 1. 18 ^Storage at: ArrIndex ! ... Continued ... Figure A.17 in text

  6. Smalltalk example (continued) 19 nextval 20 ^((DataFile next) digitValue - $0 digitValue)! 21 reset 22 ArrIndex _ 0 !! 23 |k j sum| 24 Datastore new. 25 "Initialize k" 26 [(k _ Datastore nextval) > 0] 27 whileTrue: [1 to: k do: [(j _ Datastore nextval) print. 28 Character space print. 29 Datastore asgn: j]. 30 Datastore reset. 31 sum _ 0. 32 ' SUM =' print. 33 1 to: k do: [ sum _ sum + Datastore getval]. 34 sum printNl. 35 Datastore reset] !

  7. Smalltalk execution Syntax: object message ! 3 printNl !  Send printNl message to object 3 printNl method prints its argument followed by newline (Nl). print method does same except doesn't add newline. Note naming convention: thisIsAMethodName Words strung together, all except first capitalized.

  8. Smalltalk execution (continued) The printNl method is defined for string objects: 'Hello World' printNl' !  Send printNl message to object ‘Hello World’ Each method returns an object, so can repeat process: object method1 method2 method3 ! Functions are arbitrary so no precedence - left to right evaluation: 2 + 3 * 4  20 (not the expected” 14)

  9. Methods Three kinds of methods: 1. Unary - Method name only; no arguments. E.g., printNl, print. 2. Binary - Operator between objects; builtin, E.g., 2+3 means: Send the 3 object to the + method of the 2 object. [Very different from (3 + 2)] 2 + 3 * 4! Means - send the 3 object to + of 2 object; return the 5 object. - send the 4 object to * of 5 object; return the 20 object. What happens: 2 + 3 * 4 printNl! ??? 2 + 3 results in 5 object 4 printNl results in 4 being printed. Unary operator associated with preceding object. 5 * 4 results in 20 object Use parentheses to get desired effect: (( 2 + 3) * 4 ) printNl !

  10. Keyword methods 3. Keyword methods: methodName: argumentObject If A is an array object: A at:4 put:7 ! - the at:put: method sets 4th element = 7 A at:4 - the at: method returns the 4th object in A Note naming conventions. Method at:put: may not be same as put:at: method. Other execution features: Blocks: [ smalltalkStmt. smalltalkStmt]  means return object: A returns object A

  11. True and false in Smalltalk Object true as defined in Smalltalk library: ifTrue: trueBlock ifFalse: falseBlock trueBlock value ! (value evaluates block argument) ifFalse: falseBlock ifTrue: trueBlock trueBlock value ! ifTrue: trueBlock trueBlock value ! ifFalse: falseBlock nil ! The ifTrue argument is evaluated in each case. Object false as defined in Smalltalk library: ifTrue: trueBlock ifFalse: falseBlock falseBlock value ! . . . Others in an analogous way Always evaluate the ifFalse argument

  12. Use of Smalltalk logical expressions Example method definition: hello 2 = self ifTrue:[`Hello world' printNl] ifFalse:['Goodbye world' printNl] !! 2 hello ! 2 = self  self is the argument, which is 2, so returns true true passed ifTrue:ifFalse: blocks  ifTrue block evaluated printNl passed to 'Hello world'  'Hello world' printed 3 hello ! 2 = self  self is the argument, which is 3, so returns false false passed ifTrue:ifFalse: blocks  ifFalse block evaluated printNl passed to 'Goodbye world'  'Goodbye world' printed

  13. Smalltalk inheritance So far we have discussed method invocation. Power of Smalltalk is in inheritance. Smalltalk comes with a predefined class library. All object classes are subclasses of a parent Object class. If any method is not defined for an objects, the parent of that object is called with the same method. Earlier example: 3 printNl ! not quite right. printNl a method defined on Object class.

  14. Object.st method definitions print self printOn: stdout ! printNl self print. stdout nl ! printOn: aStream | article nameString | (local variables) nameString _ self classStringName. (_ is assignment) article _ nameString firstIsVowel ifTrue:['an'] ifFalse: ['a']. aStream nextPutAll article; nextPutAll nameString ! In Integer.st: printOn: aStream base:b aStream nextPutAll: (self radix:b) ! printOn: aStream aStream nextPutAll: (self signedstringbase:10 showradix:false)!

  15. Creating methods Two types of methods: Class methods - Applies to class objects (Integer). ! ClassName class methodsFor: 'description' ! Class instance methods - Applies to instances of class objects (2, 3, 42). ! ClassName methodsFor: 'description' ! Create a new class - Use following 5-keyword method (which is why all 5 keywords are needed) to create a new subclass of class Object: Object subclass: #newClassName instanceVariableNames: instanceVariables  Local data classVariableNames: classVariables  global data poolDictionaries: ' '  Can generally ignore category: nil !  Can generally ignore

  16. Creating methods (continued) Example: A Thing which is a subclass of an Integer: Integer subclass:#Thing instanceVariableNames: 'aValue’ classVariableNames: ' ' poolDictionaries: ' ' category: nil ! # gives the name a global attribute ! Thing class methodsFor: 'Creation' ! new | r | r _ super new r init ! (super is superclass of self object. Allocate for Thing the same thing as an Integer (its parent class)) ! Thing methodsFor: 'Using things' ! init aValue _ 0 !!

  17. Method inheritance Review previous definitions of print in Object.st and show that: Integer printNl ! Prints “an Integer” 3 printNl ! prints “3”

More Related