Smalltalk Topics History and Significance of Smalltalk Object-Oriented Programming the Best Way to Predict the Future Is to the Smalltalk Language Invent It
Total Page:16
File Type:pdf, Size:1020Kb
Smalltalk Topics History and significance of Smalltalk Object-oriented programming The best way to predict the future is to The Smalltalk language invent it. Smalltalk today Additional Examples Alan Kay, 1971 Alan Kay coined the term “object History of Smalltalk oriented” Smalltalk was developed at the Xerox Palo “…and I can tell you I did not have C++ in mind.” Alto Research Center (PARC) in the early 1970’s 2004 Turing Award Winner Alan Kay, Dan Engalls, Adele Goldberg, Dave Robson Delivered the Turing Lecture at OOPSLA 2004 OOP and GUI Steve Jobs, PARC, Dec 1979 And they showed me really three things. But I was so blinded by the Smalltalk was the first object-oriented first one I didn't even really see the other two. One of the things they programming (OOP) language and the first showed me was object oriented programming; they showed me that but I didn't even see that. The other one they showed me was a graphical user interface (GUI) networked computer system...they had over a hundred Alto computers all networked using email etc., etc., I didn't even see that. I was so blinded by the first thing they showed me which was the graphical user interface. I thought it was the best thing I'd ever seen Gave Steve Jobs the idea for the in my life. Now remember it was very flawed, what we saw was MacIntosh incomplete, they'd done a bunch of things wrong. But we didn't know that at the time but still though they had the germ of the idea was there and they'd done it very well and within you know ten minutes it was obvious to me that all computers would work like this some day. Smalltalk 1 Object-Oriented Programming OOP Object: encapsulates data describing Inheritance: reuse of classes by something (often an object in the real specializing general behavior world) as well as methods (or programs) Polymorphism: many different objects can that manipulate that data respond to the same message Message: communication between objects; Dynamic Binding: resolution of message method invocations binding is deferred until runtime Class: defines structure and common behavior of a group of objects OOP Smalltalk Implementations Combination of inheritance with Smalltalk is written mostly in Smalltalk polymorphism and dynamic binding provides a powerful form of genericity The core of any Smalltalk implementation is a small bytecode interpreter (virtual Allows high levels of reuse, fast development machine) Smalltalk source code is compiled to intermediate bytecode to be executed on BUT: “Even if you only want the banana, you have to take the whole gorilla.” the VM Programming in Smalltalk Objects have local memory, inherent processing Programming in Smalltalk involves capability, capability to communicate with – Defining classes other objects. – Implementing class methods Examples: – Evaluating expressions 2 True FileStream Text Editor Most Smalltalk languages provide an Class MetaClass interactive GUI as a development Can be passed as parameters and returned as environment results Smalltalk 2 Messages and Methods Classes and Instances Class: The abstract definition of the Message: a request to an object to perform behavior of a class of objects an operation (analogous to function call) Method: implementation of an operation Instance: an individual object defined by a (function) class Examples of messages: ‘hello, world’ size #(1 12 24 36) includes: 4 factorial 3 < 4 ifTrue: [‘Yes’] ifFalse: [‘No’] Instance methods and Class methods Instance variables Instance method: a method that describes Instance variables: a variable that stores how an operation is carried out by every data for an instance of a class instance of a class Class method: a method that describes how Generally have different values for each an operation is carried out by a class, such instance as creating a new instance of itself. Class methods are sent to the class (an The collection of instance variables instance of Class MetaClass) rather than to describes the state of the object an instance of the class Class variables Inheritance A variable that is shared by a class and all of its instances Classes are arranged in a hierarchy Superclass: the parent of a class Available to all instances of a class Subclass: a child of a class Can be constants (Float pi) or even Subclasses inherit the variables and references to existing instances, such as methods of the superclass Students class that maintains a list of Student instances Smalltalk 3 The Magnitude Class Hierarchy Inheritance in Smalltalk Magnitude Association Is a strict tree structure: a subclass can Character have one and only one parent class Date Number Float Subclasses usually “specialize” the more Fraction general behavior of the parent class Integer – Can add new variables or methods LargeInteger – can also hide inherited functionality SmallInteger Time The Collection Class Hierarchy Another Collection Hierarchy Collection Bag IndexedCollection FixedSizeCollection Array Bitmap ByteArray CompiledMethod Interval String Symbol OrderedCollection Process SortedCollection Set Dictionary … 22 Expressions Literals and Variables Smalltalk does not have a “statement” in the sense used by procedural languages Computation is achieved by evaluating Literals expressions #aSymbol #(1 2 4 16 32 64) All expressions return an object ‘Hello, World’ Types: – Literals – Variable names Variables – Message expressions Smalltalk x selectedDictionary – Blocks of code Smalltalk 4 Variables Public and Private Variables Names are syntactically similar to other Public variables are shared and visible languages globally All variables are pointers to objects Name begins with upperclass letter All variables are inherently typeless Private variables are local to an object, No difference between pointing to the number block or method 42 and pointing to an instance of a text editor – Name begins with lowerclass letter Message Expressions Message Expression Examples set add: stream next Sending a message involves: (receiver is set, selector is add, argument is result of stream next, where receiver is stream and next is – object to which message is sent (“receiver”) selector) – additional objects included in message 2 + 3 (“arguments”) (receiver is 2, selector is +, argument is 3) – desired operation to be performed (“message selector”) array at: index + offset put: Bag new – Accepting the single object returned as the array at: 1 put: self “message answer” Message Syntax Types Message Syntax Types Unary (no parameters) Keyword (a general extension of infix firstAngle cos syntax) 42 PrintString myArray at: 42 put: 5 Binary (selector is “at: put:”) 2 + 3 In practice very few methods accept more thisCollection = thatCollection than 2 parameters Smalltalk 5 Selector Evaluation Order Cascading Messages A series of messages sent to same object Rules are a bit odd unless you are used to APL myPen home; up; goto: 100@200; down; home Unary and binary expressions associate left to right Equivalent to: 12 - 3 * 3 -> 27 myPen home 12 - (3 * 3) -> 3 myPen up 12 - 12 sin * 3 -> 0 myPen goto: 100@200 myPen down myPen home Method Definitions Self references Often an object needs to send a message to itself General Syntactic Form MessagePattern Pseudovariable "self" is used to refer to the object itself | local variables | Expressions Examples: Return object designated with ^ count = 0 An object can refer to itself with self ifTrue: [self error: "0 items being averaged"] ifFalse: [ ^sum / count] Self references Unary method example Fibonacci numbers fibonacci left "Answer the nth fibonacci number, where "return the left subtree of the n is the receiver" receiver" ^self < 3 ifTrue: [1] ^ self left ifFalse: [(self-1) fibonacci + (self-2) fibonacci] Testing fibonacci #(1 2 3 4 5 6 7 10 30) collect: [:m | m fibonacci] Smalltalk 6 Binary method example index := self size Binary method example index ~= aCollection size = aCollection ifTrue: [^false] "answer true if elements contained by receiver are equal to the elements [index <= 0] contained by aCollection " whileFalse: [ | index | (self at: index) = aCollection at: index) self == aCollection ifFalse: [^false]. ifTrue: [^true]. index := index – 1.] (self class == aCollection class) ^true ifFalse: [^false]. Note: == is equivalence (test for same object) while = is test for equality (equal values) Keyword method example Assignment setX: xCoord setY: yCoord setZ: zCoord Syntactically similar to other languages, but "set coordinates for a threeDpoint" variables are “typeless” x := xCoord y := yCoord z := zCoord | x | ^self x := 1 x := “A String” x := Pen new. Control Structures Blocks Smalltalk has NO conventional control Objects consisting of a sequence of structures expressions [index := index + 1. sum := sum + index.] Expression are evaluated when block Control structures are formed by passing receives the message “value” block objects as parameters to Boolean [index := index + 1. objects sum := sum + index.] value Blocks can be assigned to variables and executed by sending the message "value" to the variable. Smalltalk 7 Iteration Iteration count := 0. sum := 0. "copy a disk file" [ count <= 20 ] | input output | whileTrue: [ sum := sum + count. input : = File pathName: 'go'. count := count + 1 ] output := File pathName: 'junk'. [input atEnd] WhileTrue: is a method of Class Boolean whileFalse: [output nextPut: input next], value is a method of Class Block input close, whileTrue: sends message "value" to conditional block output close Conditional