Introduction to Smalltalk

Slides:



Advertisements
Similar presentations
PZ10B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ10B - Garbage collection Programming Language Design.
Advertisements

Introduction to C Programming
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
11. A bit of Smalltalk. © O. Nierstrasz P2 — A bit of Smalltalk 11.2 A bit of Smalltalk Overview  Some history  Smalltalk syntax & object model  The.
Principles of Object-Oriented Software Development The language Smalltalk.
2. Smalltalk Basics. © Oscar Nierstrasz ST — Smalltalk Basics 2.2 Roadmap  Everything is an Object  Syntax on a Postcard  Three Kinds of Messages 
PZ07B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ07B - Basic statements Programming Language Design.
Stéphane Ducasse 1 The Taste of Smalltalk.
13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development.
PZ06BX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ06BX - Introduction to Smalltalk Programming Language.
2. Smalltalk Basics. © Oscar Nierstrasz ST — Smalltalk Basics 2.2 Roadmap  Everything is an Object  Syntax on a Postcard  Three Kinds of Messages 
Some Basic Points on Classes
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 13 Object-Oriented Programming I am surprised.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
03G-1 Everything is an Object Examining builtin classes All these are classes in Little Smalltalk:  Object  Class  Method  Block  Boolean True False.
Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses.
Introduction to C Programming
Stéphane Ducasse5.1 Smalltalk in a Nutshell OO Model in a Nutshell Syntax in a Nutshell.
Stéphane Ducasse 1 Smalltalk in a Nutshell.
S.Ducasse Stéphane Ducasse 1 The Taste of Smalltalk.
Introduction to Ruby CSE 413 Autumn 2008 Credit: Dan Grossman, CSE341.
PZ01A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ01A -- Introduction Programming Language Design and.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
High-Level Programming Languages: C++
CS 598 Scripting Languages Design and Implementation 7. Smalltalk 80.
1 Introduction Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
CS 152: Programming Language Paradigms March 10 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Smalltalk (and Squeak) Aida Dungan and Rick Shreve.
Chapter 12 Support for Object oriented Programming.
PZ03EX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03EX - ML Programming Language Design and Implementation.
Squeak and Botkit-Port Jeff Forbes Joel Miller. Introduction ● Squeak is a pure OO language ● Based off of SmallTalk-80 – Designed at Xerox PARC by Alan.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 11 Categories of languages that support OOP: 1. OOP support is added to an existing language.
3C-1 Identifiers Variables Literal Objects  Numbers  Characters  Strings Control Structure  Blocks  Conditionals  Loops Messages Defining New Classes.
Stéphane Ducasse 1 Some Points on Classes.
S.Ducasse Stéphane Ducasse 1 Smalltalk in a Nutshell.
S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Smalltalk in a Nutshell.
CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14 – Smalltalk, Fall CSE3302 Programming Languages, UT-Arlington ©Chengkai.
Stéphane Ducasse 1 A Little Journey in the Smalltalk Syntax.
Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
1 Standardization, Internationalization Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section.
7.1 What Is An Object Object-oriented program - Description or simulation of application Object-oriented programming is done by adopting or extending an.
Expressions and Assignment
Chapter 2 - Introduction to C Programming
Class 22: Inheritance CS150: Computer Science University of Virginia
Comp 205: Comparative Programming Languages
An Introduction to Programming
ML Programming Language Design and Implementation (4th Edition)
Types of Programming Languages
Chapter 2 - Introduction to C Programming
Procedural Abstraction Object-Oriented Code
PZ10CX - LISP Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section Appendix A.6.
Feedback from Assignment 1
Smalltalk – a Pure (and first) OOPL
Object-Oriented Programming
CS 331 Principles of Programming Languages
Introduction to Smalltalk
Overview of Programming Paradigms
강의 내용 및 방법 접근방법 리포트 시험 Lambda Calculus, Proof of Correctness
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
An Introduction to Programming
In this class, we will cover:
Introduction to C Programming
Garbage collection Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
CSE 3302 Programming Languages
Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
PZ07B - Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Smalltalk on a Dime.
Presentation transcript:

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. 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.

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

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.

Smalltalk example Figure A.17 in text 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

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] !

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.

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)

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 !

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

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 ifFalse: falseBlock nil ! The ifTrue argument is evaluated in each case. Object false as defined in Smalltalk library: falseBlock value ! . . . Others in an analogous way Always evaluate the ifFalse argument

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

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.

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) ! aStream nextPutAll: (self signedstringbase:10 showradix:false)!

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

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 !!

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