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.

Slides:



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

Dynamic Object-Oriented Programming with Smalltalk 1. Introduction Prof. O. Nierstrasz.
8. Introduction to Denotational Semantics. © O. Nierstrasz PS — Denotational Semantics 8.2 Roadmap Overview:  Syntax and Semantics  Semantics of Expressions.
12. Common Errors, a few Puzzles. © O. Nierstrasz P2 — Common Errors, a few Puzzles 12.2 Common Errors, a few Puzzles Sources  Cay Horstmann, Computing.
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
Dynamic Object-Oriented Programming with Smalltalk 1. Introduction Prof. O. Nierstrasz Autumn Semester 2009.
Principles of Object-Oriented Software Development The language Smalltalk.
Dynamic Object-Oriented Programming with Smalltalk 1. Introduction Prof. O. Nierstrasz Autumn Semester 2007.
Object-Oriented Reengineering Patterns and Techniques Prof. O. Nierstrasz Prof. S. Ducasse T.
2. Smalltalk Basics. © Oscar Nierstrasz ST — Smalltalk Basics 2.2 Roadmap  Everything is an Object  Syntax on a Postcard  Three Kinds of Messages 
7. Understanding Classes and Metaclasses. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.2 Roadmap  Metaclasses in 7 points  Indexed.
12. Common Errors, a few Puzzles. © O. Nierstrasz P2 — Common Errors, a few Puzzles 12.2 Common Errors, a few Puzzles Overview  Common errors … —Typical.
ESE Einführung in Software Engineering N. XXX Prof. O. Nierstrasz Fall Semester 2009.
© Oscar Nierstrasz ST — Smalltalk Basics 2.1 Change sets  Make sure your changes are logged to a new change set.
13. Summary, Trends, Research. © O. Nierstrasz PS — Summary, Trends, Research Summary, Trends, Research...  Summary: functional, logic and object-oriented.
Stéphane Ducasse 1 The Taste of Smalltalk.
Dynamic Object-Oriented Programming with Smalltalk 1. Introduction Prof. O. Nierstrasz Summer Semester 2006.
13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development.
Dynamic Object-Oriented Programming with Smalltalk 1. Introduction Prof. O. Nierstrasz Summer Semester 2006.
Metamodeling Seminar X. CHAPTER Prof. O. Nierstrasz Spring Semester 2008.
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 
© Oscar Nierstrasz ST — Smalltalk Basics 2.1 Change sets  Make sure your changes are logged to a new change set.
S.Ducasse Stéphane Ducasse 1 A Taste of Smalltalk.
ESE Einführung in Software Engineering X. CHAPTER Prof. O. Nierstrasz Wintersemester 2005 / 2006.
6. Debugging. © Oscar Nierstrasz ST — Debugging 6.2 Roadmap  Common syntactic errors  Common semantic errors  Encapsulation errors  Class/instance.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 13 Object-Oriented Programming I am surprised.
N. XXX Prof. O. Nierstrasz Thanks to Jens Palsberg and Tony Hosking for their kind permission to reuse and adapt the CS132 and CS502 lecture notes.
12. Common Errors, a few Puzzles. © O. Nierstrasz P2 — Common Errors, a few Puzzles 12.2 Common Errors, a few Puzzles Sources  Cay Horstmann, Computing.
6. Debugging. © Oscar Nierstrasz ST — Debugging 6.2 Roadmap  Common syntactic errors  Common semantic errors  Encapsulation errors  Class/instance.
03G-1 Everything is an Object Examining builtin classes All these are classes in Little Smalltalk:  Object  Class  Method  Block  Boolean True False.
JavaScript, Third Edition
Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses.
© S. Demeyer, S. Ducasse, O. Nierstrasz Chapter.1 MakeMoney Corp. C*O of MakeMoney Corp. Our Vision  We invest in software  We do not know software 
OORPT Object-Oriented Reengineering Patterns and Techniques X. CHAPTER Prof. O. Nierstrasz.
CP — Concurrent Programming X. CHAPTER Prof. O. Nierstrasz Wintersemester 2005 / 2006.
3. Standard Classes. © Oscar Nierstrasz ST — Standard Classes 3.2 Roadmap  Object  Numbers, Characters, Strings and Arrays  Variables  Blocks and.
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.
12. eToys. © O. Nierstrasz PS — eToys 12.2 Denotational Semantics Overview:  … References:  …
Stéphane Ducasse 1 Smalltalk in a Nutshell.
Stéphane Ducasse4.1 A Taste of Smalltalk Two examples: –‘hello world’ –A Tomagoshi To give you an idea of: –the syntax –the elementary objects and classes.
S.Ducasse Stéphane Ducasse 1 The Taste of Smalltalk.
Introduction to Ruby CSE 413 Autumn 2008 Credit: Dan Grossman, CSE341.
3A-1 1 Introduction to Smalltalk History of Smalltalk The philosophy of Smalltalk:  “...we have a universe of well-behaved objects that courteously ask.
CSE 3302 Programming Languages Chengkai Li Spring 2008 Object-Oriented Programming Lecture 13 – OO Programming, Spring CSE3302 Programming Languages,
Chapter 3: Data Types and Operators JavaScript - Introductory.
CS 598 Scripting Languages Design and Implementation 7. Smalltalk 80.
Smalltalk for Unix & W i n d o w s Dennis Smith Cherniak Software Development Corporation.
Smalltalk (and Squeak) Aida Dungan and Rick Shreve.
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.
Smalltalk in a.NET World How to write a Smalltalk compiler without writing a VM John Brant
3C-1 Identifiers Variables Literal Objects  Numbers  Characters  Strings Control Structure  Blocks  Conditionals  Loops Messages Defining New Classes.
1 Advanced Object Oriented Systems (CM0318) Lecture 8 (Last updated 15th February 2002)
3. Standard Classes. © Oscar Nierstrasz ST — Standard Classes 3.2 Roadmap  Object  Numbers, Characters, Strings and Arrays  Variables  Blocks and.
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.
Stéphane Ducasse 1 Abstract Classes.
7.1 What Is An Object Object-oriented program - Description or simulation of application Object-oriented programming is done by adopting or extending an.
Aaron Bloomfield CS 415 Fall 2005
Smalltalk – a Pure (and first) OOPL
Chapter 2 Squeak: Object-oriented design with multimedia applications
CS 331 Principles of Programming Languages
Introduction to Smalltalk
Let’s Talk about… Smalltalk.
Introduction to Smalltalk
CSE 3302 Programming Languages
Smalltalk on a Dime.
Presentation transcript:

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 Smalltalk environment (demo) References:  en.wikipedia.org/wiki/Smalltalk en.wikipedia.org/wiki/Smalltalk  squeak.org/documentation/ squeak.org/documentation/

© O. Nierstrasz P2 — A bit of Smalltalk 11.3 Essential Smalltalk Texts  Smalltalk by Example, Alec Sharp, McGraw Hill, 1997 [  PDF available below]  The Smalltalk Design Pattern Companion, S. Alpert, K. Brown and B. Woolf, Addison-Wesley, 1998  Smalltalk Best Practice Patterns, K. Beck, Prentice Hall, 1997  Squeak, X. Briffault, S. Ducasse, Eyrolles, 2001 [in French]  Squeak, Open Personal Computing and Multimedia, Kim Rose and Mark Guzdial, Prentice-Hall  More:

© O. Nierstrasz P2 — A bit of Smalltalk 11.4 Available Smalltalks  Squeak —  Cincom VisualWorks Smalltalk —smalltalk.cincom.comsmalltalk.cincom.com  Dolphin Smalltalk —  Gnu Smalltalk —  Others … —en.wikipedia.org/wiki/Smalltalken.wikipedia.org/wiki/Smalltalk

© O. Nierstrasz P2 — A bit of Smalltalk 11.5 History

© O. Nierstrasz P2 — A bit of Smalltalk 11.6 Origins of Smalltalk  Project at Xerox PARC in 1970s —Language and environment for new generation of graphical workstations (target: “Dynabook”)  In Smalltalk-72, every object was an independent entity —Language was designed for children (!) —Evolved towards a meta-reflective architecture  Smalltalk-80 is the standard  See: The Early History of Smalltalk, by Alan Kay —gagne.homedns.org/~tgagne/contrib/EarlyHistoryST.htmlgagne.homedns.org/~tgagne/contrib/EarlyHistoryST.html

© O. Nierstrasz P2 — A bit of Smalltalk 11.7 What is Smalltalk?  Guiding principle: “Everything is an Object”  Language and environment —Class browser, debugger, inspector, … —Mature class library and tools  Virtual machine —Objects exist in a persistent image [+ changes] —Incremental compilation  Pure OO language —Single inheritance —Dynamically typed

© O. Nierstrasz P2 — A bit of Smalltalk 11.8 Smalltalk vs. C++ vs. Java SmalltalkC++Java Object modelPureHybrid Garbage collection AutomaticManualAutomatic InheritanceSingleMultipleSingle TypesDynamicStatic ReflectionFully reflectiveIntrospection ConcurrencySemaphoresSome librariesMonitors ModulesCategories, namespaces NamespacesPackages

© O. Nierstrasz P2 — A bit of Smalltalk 11.9 “Hello World”

© O. Nierstrasz P2 — A bit of Smalltalk Smalltalk Syntax exampleWithNumber: x "A method that illustrates every part of Smalltalk method syntax except primitives. It has unary, binary, and key word messages, declares arguments and temporaries (but not block temporaries), accesses a global variable (but not and instance variable), uses literals (array, character, symbol, string, integer, float), uses the pseudo variable true false, nil, self, and super, and has sequence, assignment, return and cascade. It has both zero argument and one argument blocks. It doesn’t do anything useful, though" |y| true & false not & (nil isNil) ifFalse: [self halt]. y := self size + super size. #($a #a 'a' 1 1.0) do: [:each | Transcript show: (each class name); show: (each printString); show: ' ']. ^ x < y exampleWithNumber: x "A method that illustrates every part of Smalltalk method syntax except primitives. It has unary, binary, and key word messages, declares arguments and temporaries (but not block temporaries), accesses a global variable (but not and instance variable), uses literals (array, character, symbol, string, integer, float), uses the pseudo variable true false, nil, self, and super, and has sequence, assignment, return and cascade. It has both zero argument and one argument blocks. It doesn’t do anything useful, though" |y| true & false not & (nil isNil) ifFalse: [self halt]. y := self size + super size. #($a #a 'a' 1 1.0) do: [:each | Transcript show: (each class name); show: (each printString); show: ' ']. ^ x < y

© O. Nierstrasz P2 — A bit of Smalltalk Literals and constants Strings & Characters'hello' $a Numbers Symbols#yadayada Arrays#(1 2 3) Pseudo-variablesself super Constantstrue false

© O. Nierstrasz P2 — A bit of Smalltalk Fun with Numbers  Large and exact numbers  Automatic coercion 1000 factorial / 999 factorial 1000 factorial printString size 1000 factorial / 999 factorial 1000 factorial printString size (1/3) + (2/3) 1 class maxVal (1 class maxVal + 1) class (1/3) + (2/3) 1 class maxVal (1 class maxVal + 1) class LargePositiveInteger LargePositiveInteger

© O. Nierstrasz P2 — A bit of Smalltalk “Natural language” album play album playTrack: 1 album playFromTrack: 5 to: 10 album play album playTrack: 1 album playFromTrack: 5 to: 10

© O. Nierstrasz P2 — A bit of Smalltalk Smalltalk Syntax Every expression is a message send  Unary messages  Binary messages  Keyword messages Transcript cr 5 factorial Transcript cr 5 factorial Transcript show: 'hello world' 2 raisedTo: 32 3 raisedTo: 10 modulo: 5 Transcript show: 'hello world' 2 raisedTo: 32 3 raisedTo: 10 modulo: 5

© O. Nierstrasz P2 — A bit of Smalltalk Precedence (…) > Unary > Binary > Keyword 1. Evaluate left-to-right 2. Unary messages have highest precedence 3. Next are binary messages 4. Keyword messages have lowest precedence 5. Use parentheses to change precedence 2 raisedTo: factorial * (2 * 3) * (2 * 3) 128 9(!) 7 9(!) 7

© O. Nierstrasz P2 — A bit of Smalltalk More syntax  Comments are enclosed in double quotes  Use periods to separate expressions  Use semi-colons to send a cascade of messages to the same object "This is a comment." Transcript cr; show: 'hello world'; cr Transcript cr. Transcript show: 'hello world’. Transcript cr "NB: don’t need one here" Transcript cr. Transcript show: 'hello world’. Transcript cr "NB: don’t need one here"

© O. Nierstrasz P2 — A bit of Smalltalk Variables  Declare local variables with | … |  Use := or  (_) to assign a value to a variable | x y | x := 1

© O. Nierstrasz P2 — A bit of Smalltalk Method Return  Use a caret to return a value from a method or a block  By default, methods return self max: aNumber ^ self < aNumber ifTrue: [aNumber] ifFalse: [self] max: aNumber ^ self < aNumber ifTrue: [aNumber] ifFalse: [self] 1 max: 2 2 2

© O. Nierstrasz P2 — A bit of Smalltalk Block closures  Use square brackets to delay evaluation of expressions  Use a caret to return a value from a method or a block 1 < 2 ifTrue: [^'smaller'] ifFalse: [^'bigger'] ^ 1 < 2 ifTrue: ['smaller'] ifFalse: ['bigger'] 'smaller'

© O. Nierstrasz P2 — A bit of Smalltalk Variables  Local variables are delimited by |var| Block variables by :var| OrderedCollection>>collect: aBlock "Evaluate aBlock with each of my elements as the argument." | newCollection | newCollection := self species new: self size. firstIndex to: lastIndex do: [ :index | newCollection addLast: (aBlock value: (array at: index))]. ^ newCollection OrderedCollection>>collect: aBlock "Evaluate aBlock with each of my elements as the argument." | newCollection | newCollection := self species new: self size. firstIndex to: lastIndex do: [ :index | newCollection addLast: (aBlock value: (array at: index))]. ^ newCollection (OrderedCollection with: 10 with: 5) collect: [:each| each factorial ] an OrderedCollection( )

© O. Nierstrasz P2 — A bit of Smalltalk Control Structures (I)  Every control structure is realized by message sends max: aNumber ^ self < aNumber ifTrue: [aNumber] ifFalse: [self] max: aNumber ^ self < aNumber ifTrue: [aNumber] ifFalse: [self] 4 timesRepeat: [Beeper beep]

© O. Nierstrasz P2 — A bit of Smalltalk Control Structures  Every control structure is realized by message sends |n| n := 10. [n>0] whileTrue: [Transcript show: n; cr. n := n-1] |n| n := 10. [n>0] whileTrue: [Transcript show: n; cr. n := n-1] 1 to: 10 do: [:n| Transcript show: n; cr ] (1 to: 10) do: [:n| Transcript show: n; cr ]

© O. Nierstrasz P2 — A bit of Smalltalk Creating objects  Class methods  Factory methods OrderedCollection new Array with: 1 with: 2 OrderedCollection new Array with: 1 with: 2 1/2 1/2 a Point a Fraction a Point a Fraction

© O. Nierstrasz P2 — A bit of Smalltalk Creating classes  Send a message to a class (!) Number subclass: #Complex instanceVariableNames: 'real imaginary' classVariableNames: '' poolDictionaries: '' category: 'ComplexNumbers' Number subclass: #Complex instanceVariableNames: 'real imaginary' classVariableNames: '' poolDictionaries: '' category: 'ComplexNumbers'

© O. Nierstrasz P2 — A bit of Smalltalk The Smalltalk Browser

© O. Nierstrasz P2 — A bit of Smalltalk The Debugger

© O. Nierstrasz P2 — A bit of Smalltalk The Inspector

© O. Nierstrasz P2 — A bit of Smalltalk Other Tools  File List —Browse, import, open files  Change Sorter —Name, organize all source code changes  Method Finder, Message Name tool —Find methods by name, behaviour  SUnit —Manage & run unit tests

© O. Nierstrasz P2 — A bit of Smalltalk Demo Changing a running system …

© O. Nierstrasz P2 — A bit of Smalltalk What you should know!  What are the key differences between Smalltalk, C++ and Java?  What is at the root of the Smalltalk class hierarchy?  What kinds of messages can one send to objects?  What is a cascade?  Why does 1 + 2/3 = 1 in Smalltalk?  How are control structures realized?  How is a new class created?  What are categories for?  What are Factory methods? When are they useful?

© O. Nierstrasz P2 — A bit of Smalltalk Can you answer these questions?  Which is faster, a program written in Smalltalk, C++ or Java?  Which is faster to develop & debug, a program written in Smalltalk, C++ or Java?  How are Booleans implemented?  Is a comment an Object? How would you check this?  What is the equivalent of a static method in Smalltalk?  How do you make methods private in Smalltalk?  What is the difference between = and ==?  If classes are objects too, what classes are they instances of?

© O. Nierstrasz P2 — A bit of Smalltalk License > Attribution-ShareAlike 2.5 You are free: to copy, distribute, display, and perform the work to make derivative works to make commercial use of the work Under the following conditions: Attribution. You must attribute the work in the manner specified by the author or licensor. Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one. For any reuse or distribution, you must make clear to others the license terms of this work. Any of these conditions can be waived if you get permission from the copyright holder. Your fair use and other rights are in no way affected by the above. Attribution-ShareAlike 2.5 You are free: to copy, distribute, display, and perform the work to make derivative works to make commercial use of the work Under the following conditions: Attribution. You must attribute the work in the manner specified by the author or licensor. Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one. For any reuse or distribution, you must make clear to others the license terms of this work. Any of these conditions can be waived if you get permission from the copyright holder. Your fair use and other rights are in no way affected by the above.