Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 11. A bit of Smalltalk

2 © 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/

3 © 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 2000.  More: www.iam.unibe.ch/~ducasse/FreeBooks.htmlwww.iam.unibe.ch/~ducasse/FreeBooks.html

4 © O. Nierstrasz P2 — A bit of Smalltalk 11.4 Available Smalltalks  Squeak —www.squeak.orgwww.squeak.org  Cincom VisualWorks Smalltalk —smalltalk.cincom.comsmalltalk.cincom.com  Dolphin Smalltalk —www.object-arts.com/Home.htmwww.object-arts.com/Home.htm  Gnu Smalltalk —www.gnu.org/software/smalltalk/smalltalk.htmlwww.gnu.org/software/smalltalk/smalltalk.html  Others … —en.wikipedia.org/wiki/Smalltalken.wikipedia.org/wiki/Smalltalk

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

6 © 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

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

8 © 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

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

10 © O. Nierstrasz P2 — A bit of Smalltalk 11.10 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

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

12 © O. Nierstrasz P2 — A bit of Smalltalk 11.12 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 1000 2568 1000 2568 1 1073741823 LargePositiveInteger 1 1073741823 LargePositiveInteger

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

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

15 © O. Nierstrasz P2 — A bit of Smalltalk 11.15 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: 1 + 3 factorial 1 + 2 * 3 1 + (2 * 3) 1 + 2 * 3 1 + (2 * 3) 128 9(!) 7 9(!) 7

16 © O. Nierstrasz P2 — A bit of Smalltalk 11.16 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"

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

18 © O. Nierstrasz P2 — A bit of Smalltalk 11.18 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

19 © O. Nierstrasz P2 — A bit of Smalltalk 11.19 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'

20 © O. Nierstrasz P2 — A bit of Smalltalk 11.20 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(3628800 120)

21 © O. Nierstrasz P2 — A bit of Smalltalk 11.21 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]

22 © O. Nierstrasz P2 — A bit of Smalltalk 11.22 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 ]

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

24 © O. Nierstrasz P2 — A bit of Smalltalk 11.24 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'

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

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

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

28 © O. Nierstrasz P2 — A bit of Smalltalk 11.28 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

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

30 © O. Nierstrasz P2 — A bit of Smalltalk 11.30 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?

31 © O. Nierstrasz P2 — A bit of Smalltalk 11.31 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?

32 © O. Nierstrasz P2 — A bit of Smalltalk 11.32 License > http://creativecommons.org/licenses/by-sa/2.5/ 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.


Download ppt "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."

Similar presentations


Ads by Google