CS 598 Scripting Languages Design and Implementation 7. Smalltalk 80.

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Introduction to Ruby.
Advertisements

1 Object Orientation James Brucker. 2 Smalltalk  Ahead of its time: consistent design and rich library.  Dynamic (like Lisp): variables have no specified.
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 
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
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 
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 13 Object-Oriented Programming I am surprised.
Data types and variables
JavaScript, Fourth Edition
JavaScript, Third Edition
Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses.
Stéphane Ducasse5.1 Smalltalk in a Nutshell OO Model in a Nutshell Syntax in a Nutshell.
12. A bit of Smalltalk. © O. Nierstrasz P2 — A bit of Smalltalk 11.2 Roadmap  Some history  Smalltalk syntax & object model  The Smalltalk environment.
VB .NET Programming Fundamentals
Stéphane Ducasse 1 Smalltalk in a Nutshell.
S.Ducasse Stéphane Ducasse 1 The Taste of Smalltalk.
Computer Science 1000 Spreadsheets II Permission to redistribute these slides is strictly prohibited without permission.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
3A-1 1 Introduction to Smalltalk History of Smalltalk The philosophy of Smalltalk:  “...we have a universe of well-behaved objects that courteously ask.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
2440: 211 Interactive Web Programming Expressions & Operators.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Input, Output, and Processing
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Smalltalk (and Squeak) Aida Dungan and Rick Shreve.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
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.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
CS 598 Scripting Languages Design and Implementation 8. Self.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Programming with Microsoft Visual Basic th Edition
3C-1 Identifiers Variables Literal Objects  Numbers  Characters  Strings Control Structure  Blocks  Conditionals  Loops Messages Defining New Classes.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
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.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Stéphane Ducasse 1 A Little Journey in the Smalltalk Syntax.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Smalltalk Meta-Programming Programming Languages HUJI 2010 Yardena Meymann.
Topics Designing a Program Input, Processing, and Output
Chapter 6 JavaScript: Introduction to Scripting
7.1 What Is An Object Object-oriented program - Description or simulation of application Object-oriented programming is done by adopting or extending an.
Chapter 3 Syntax, Errors, and Debugging
Java Primer 1: Types, Classes and Operators
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Introduction to C++ Programming
CS 331 Principles of Programming Languages
Introduction to Smalltalk
Chapter 2: Introduction to C++.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Chap 2. Identifiers, Keywords, and Types
Introduction to Smalltalk
CSE 3302 Programming Languages
Corresponds with Chapter 5
Presentation transcript:

CS 598 Scripting Languages Design and Implementation 7. Smalltalk 80

Smalltalk From the Wikipedia: – Smalltalk is an object-oriented, dynamically typed, reflective programming language. Smalltalk was created as the language to underpin the "new world" of computing exemplified by "human–computer symbiosis.” – It was designed and created in part for educational use, more so for constructionist learning, at the Learning Research Group (LRG) of Xerox PARC by Alan Kay, Dan Ingalls, Adele Goldberg, Ted Kaehler, Scott Wallace, and others during the 1970s. 2

Source of the material in these slides Much of the text in these slides is taken verbaim from the blue book: 3

Implementations today There are multiple implementations, (see ) including: – Amber Smalltalk Smalltalk running atop JavaScript – GNU Smalltalk – Pharo Smalltalk, Pharo Project's open-source multi-platform Smalltalk ( ) Watch: – Squeak, open source Smalltalk 4

Smalltalk Rules Rule 1. Everything is an object. Rule 2. Every object is an instance of a class. Rule 3. Every class has a superclass. Rule 4. Everything happens by sending messages. Rule 5. Method lookup follows the inheritance chain (single inheritance) 5

‘Hello, world!’ example 6 Object message

Objects Components of the software system representing – Numbers – Character strings – Queues – Dictionaries – Programs – Compilers Consist of – some private memory and – a set of operations

Messages, receivers, and interfaces A message is a request for an object to carry out one of its operations. The receiver, the object to which the message was sent, determines how to carry out the requested operation The interface of an object with the rest of the system is the set of messages to which an object can respond. An important part of designing Smalltalk-80 programs is determining which kinds of objects should be described and which message names provide a useful vocabulary of interaction between these objects.

Messages examples unary messages – examples : new, copy, size Date today Time now hours anArray size someCollection copy keyword messages – examples: new:, at:, at: put: Array new: 10 someArray at: 1 put: 54 anArray at: 1 binary messages – examples: + - * / 5 * * 5

Classes and instances A class describes the implementation of a set of objects that all represent the same kind of system component. The individual objects described by a class are called its instances Every object is an instance of a class.

Methods and instance variables The methods of an object describe how to carry out its operations. Messages cause methods to be executed. The instances of a class have the same set of methods. Each instance has its own set of instance variables, but they generally have the same number of instance variables.

Expressions components of expressions: – Literals which are constant object such as numbers and character strings – Variable names. These are the only expressions whose validity is context dependent – Message expressions which describe messages to receivers. – Block expressions which describe objects representing computations to be carried out when invoked. Expressions appear in methods and in the console (Playground).

Expression syntax 13

14

Literals

Numbers – Integers: regular numbers such as 42 and radix notation. Thus, 2r101 is 5 in binary. – Floating point numbers can be specified with their base-ten exponent (e.g. 2.4e7 ) Characters: Introduced with the $ sign. $a is the literal for ’a’. Also obtained sending messages to the Character class (e.g. Character space and Character tab )

Literals Strings. Use single quotes Symbols: Like strings, but unlike strings are guaranteed to be globally unique. Use # to identify them (e.g. #Hello ). Compile-time (constant) Arrays: Preceded by # and enclosed in parenthesis (e.g. #(27 (true false) abc) )

Variables Local variables: start with lower case letters Shared variables are identifiers that start with upper case letters and represent global variables, class variables, pool dictionaries and class names. Pseudo variables: nil, true, false, self, super, and thisContext

Examples Consider the class Number. is an expression that sends the with parameter 2 to the number 3. 19

More examples of Messages A message requests an operation from the receiver. Messages to numbers can represent arithmetic operations. – 3+4. – index + 1. Messages to linear data structures can represent the addition or removal of information – list := LinkedList new. – list addFirst: newComponent. Messages to associative data structures can be used to add or remove entries – Ages:= Dictionary new. – ages at: ‘Jonathan’ put: 6 Messages to rectangles represent graphical enquires – frame center 20 Message to list Message to ages

Selectors and Arguments A message expression describes a receiver, selector, and possibly some arguments. In 3+4, – 3 is the receiver – + is the selector – 4 the argument. Unary messages are message without arguments: – HoseholdFinances cashOnHand Keyword Messages have arguments identified by keywords. – HouseholdFinances totalSpentOn: ‘utilities’ – Ages at: ‘Jonathan’ put: 3 Binary Messages are those that contain one or two nonalphanumeric characters – total <= max –

Returning values The receiver transmit information back by returning an object that becomes the value of the message expression. – sum := 3+4 The receiver always returns a value for the message expression. When no value is needed, the receiver returns itself 22

Precedence Unary messages are always sent first, then binary messages, and finally keyword messages Parenthesized expressions have precedence over unary message – 3+4 factorial → 27 – (3+4) factorial → 5040 Messages of the same kind are evaluated from left to right 23

Cascading A cascaded message expression consist of one description of the receiver followed by several messages separated by semicolons – c:= LinkedList new. – c add: 1; add: 2; add: 3. – LinkedList new add: 1; add: 7; add: 3; at: 2 → 7 24

Blocks Objects used in many of the control structures. Represents a deferred sequence of actions It is a sequence of expressions separated by periods and delimited by []s – [index:= index+1. array at: index put: 0] The sequence of actions take place when the block receives the unary message value. Can be assigned to variables – t := [x+1] – t value Blocks may take parameters, which are declared with a leading colon – [:x | 1+x] value: 2 25

Blocks (cont.) They may also declare local variables. – [:x :y ||z| z:=x+y. z] value: 1 value: 2 → 3 Blocks are actually lexical closures since they can refer to variables in the surrounding environment. |x| x:=1. [:y | x+y] value: 2 → 3 26

Control structures Nonsequential control structures can be implemented with blocks Numbers and Booleans accept messages to repetitively execute a block i:=1. 4 timesRepeat: [i:=i+1] i:=1. [i <= list size] whileTrue: [list at: i put: 0. i:=i+1] Result:= ‘ ‘ 1 to: 10 do: [:n| result:=result, n printString, ‘ ‘] #( ) collect: [ :p|p*p ] → #( ) 27

Conditionals Conditionals are expressed by sending one of the messages ifTrue: ifFalse:, or ifTrue:ifFalse: to a Boolean – 17*13>220 ifTrue:[‘bigger'] ifFalse: ['smaller'] → ‘bigger' 28 Argument is a block

Classes and instances Every object in Smalltalk-80 is an instance of a class Each operation is described by a method. A class has a method for each selector in its interface A protocol are groups of messages understood by objects. Each class has one or more protocols. The protocols indicate the intent of the messages. Some common protocol names have been established by convention (e.g. accessing, initialization) Protocols are also called categories in the Blue Book 29

Implementation descriptions To describe a class we use – A name – A declaration of variables available to the instances – The methods used by the instances to respond to messages To find out the class of an object, you can send the class message – 1 class → SmallInteger – 20 factorial class → largePositiveInteger – class → Point 30

Variables Five classes 1.Instance variables which exist for the lifetime of the object 2.Temporary variables which exist for the lifetime of the scope 3.Class variables which are shared by all instances of a class 4.Global variables which are shared by all instances of all classes 5.Pool variables which are shared by the instances of a subset of the classes 31

Instance variables Are declared in each class Are not typed They are private to the instances of the class (and its subclasses) Can be access by the instance methods of the class 32

Methods A method describes how an object will perform its operations Example 1 Point»dist: aPoint "Answer the distance between aPoint and the receiver." | dx dy | dx := aPoint x − x. dy := aPoint y − y. ^ ((dx * dx) + (dy * dy)) sqrt dist: →

Methods (cont.) Example 2 FinancialHistory»spend: amount for: reason expenditure at: reason put: (self totalSpentFor: reason)+ amount. cashOnHand := cashOnHand – amount To return a value, we can use the ^ character Example 3: FinancialHistory»totalSpentFor: reason (expenditures includesKey: reason) ifTrue: [^expenditures at: reason] ifFalse[^0] 34

The pseudo variable self All methods have access to the pseudo variable self that refers to the receiver of the message itself. Example Integer»factorial "Answer the factorial of the receiver." self = 0 ifTrue: [^ 1]. self > 0 ifTrue: [^ self * (self - 1) factorial]. self error: 'Not valid for negative integers' 35

Temporary variables The argument names and self are available only during execution of a method. In addition, the method may obtain other variable during its execution. Example FinancialHistory»spend: amount for: reason | previousExpenditures | previousExpenditures:= self totalSpentFor: reason. expenditures at:reason put: previousExpenditures + amount. cashOnHand := cashOnHand – amount. 36

Temporary Variables They can also be created in a Playground Example |list| list:=Array new: 3. list at:1 put ‘one’. list at: 2 put ‘two’. list printString. 37

Primitive methods Implemented not as Smalltalk code, but in the virtual machine. Type at the beginning of the method to invoke it If the primitive fails, the rest of the method is executed, typically to report an error See e.g. SmallInteger » + 38

Metaclasses Since classes are objects, they are instances of other classes called metaclasses. There is a metaclass for each class. The methods of the metaclass are called class methods And the variables in a metaclass are called Class instance variables 39

Class methods They apply to classes Example aColor := Color blue. "Class side method blue" aColor → Color blue aColor red → 0.0 "Instance side accessor method red" aColor blue → 1.0 "Instance side accessor method blue" 40

Class instance variables These are instance variables declared in the metaclass. They are private to the class in the same way that instance variables are private to the instances. – Instances of a class X cannot access class instance variables of X. Subclasses have their own copies of the class instance variables. 41

Class instance variable examples Object subclass: #Dog instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'PBE−CIV‘. Dog class instanceVariableNames: 'count‘. Dog subclass: #Hyena instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'PBE−CIV‘. Dog initialize. Hyena initialize. Dog count → 0 Hyena count → 0 Dog new. Dog count → 1 Dog new. Dog count → 2 Hyena new. Hyena count → 1 42 Dog class»initialize super initialize. count := 0. Dog class»new count := count +1. ^ super new Dog class»count ^ count

Superclasses Every class has a super class. Single inheritance Methods can be overridden Search proceeds in the superclass chain, terminating in Object (or ProtoObject in the case of Pharo). If no matching method is found, the receiver is sent the message doesNotUnderstand: 43

A example of class Copied from somewhere First we define a new class. Object subclass: #CS598Stack instanceVariableNames: 'anArray top' classVariableNames: '' poolDictionaries: '' Now define some methods: push: item top := top+1. anArray at: top put: item 44 pop | item | item := anArray at: top. top := top-1. ^ item setsize: n anArray := Array new: n. top := 0. Some code to test the stack: s := CS598Stack new. s setsize: 10. s inspect. s push: 'hi there'. s push: s pop

Profiling Can measure total time | coll | coll := #( ) asOrderedCollection. [ timesRepeat: [ (coll select: [:each | each > 5]) collect: [:i|i*i]]] timeToRun. Can profile time per method MessageTally spyOn: [ 500 timesRepeat: [ | str | str := '’ timesRepeat: [ str := str, 'A' ]]]. 45

Profinling (cont.) And number of sends MessageTally tallySends: [ 1000 timesRepeat: [ printString]]. See chapter 17 in 46

Reflection From wikipedia entry on reflection: – In computer science, reflection is the ability of a computer program to examine … and modify its own structure and behavior (specifically the values, meta- data, properties and functions) at runtime – reflection allows inspection of classes, interfaces, fields and methods at runtime without knowing the names of the interfaces, fields, methods at compile time. – Reflection is also a key strategy for metaprogramming. 47

Reflection (Cont.) From wikipedia entry on metaprogramming – Metaprogramming is the writing of computer programs with the ability to treat programs as their data. It means that a program could be designed to read, generate, analyse or transform other programs, and even modify itself while running. – In some cases, this allows programmers to minimize the number of lines of code to express a solution (hence reducing development time)[citation needed], or it gives programs greater flexibility to efficiently handle new situations without recompilation. – The language in which the metaprogram is written is called the metalanguage. The language of the programs that are manipulated is called the object language. The ability of a programming language to be its own metalanguage is called reflection or reflexivity 48

Reflexion in Pharo We can see instance variables using inpect: w := Workspace new. w openLabel: 'My Workspace'. w inspect – Or their name w instVarNamed: #contents – Or their position w instVarAt: 10 Now, we can modify it – using the name of the instance variable: w instVarNamed: #contents put: 'howdy!'; changed: #contents – or its position: w instVarAt: 10 put: ‘epa!'; changed: #contents. 49

You can find out the number and name of instance variables by inspecting the class class instVarNames  #(‘x’ ‘y’) class instSize  2 50

You can also add methods dynamically: DynamicAcccessors»doesNotUnderstand: aMessage | messageName | messageName := aMessage selector asString. (self class instVarNames includes: messageName) ifTrue: [ self class compile: messageName, String cr, ' ^ ', messageName. ^ aMessage sendTo: self ]. ^ super doesNotUnderstand: aMessage 51