![Page 1: S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Smalltalk in a Nutshell](https://reader035.vdocuments.us/reader035/viewer/2022062600/5a4d1b537f8b9ab0599a8765/html5/thumbnails/1.jpg)
S.Ducasse 1
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
Stéphane [email protected]://www.iam.unibe.ch/~ducasse/
Smalltalk in a Nutshell
![Page 2: S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Smalltalk in a Nutshell](https://reader035.vdocuments.us/reader035/viewer/2022062600/5a4d1b537f8b9ab0599a8765/html5/thumbnails/2.jpg)
S.Ducasse 2
Goals•Syntax in a Nutshell•OO Model in a Nutshell
![Page 3: S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Smalltalk in a Nutshell](https://reader035.vdocuments.us/reader035/viewer/2022062600/5a4d1b537f8b9ab0599a8765/html5/thumbnails/3.jpg)
S.Ducasse 3
Smalltalk OO Model***Everything*** is an object
Only message passing Only late binding
Instance variables are private to the object and protected to the subclassesMethods are publicEverything is a pointer
Garbage collectorSingle inheritance between classesOnly message passing between objects
![Page 4: S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Smalltalk in a Nutshell](https://reader035.vdocuments.us/reader035/viewer/2022062600/5a4d1b537f8b9ab0599a8765/html5/thumbnails/4.jpg)
S.Ducasse 4
Complete Syntax on a PostCardexampleWithNumber: x
“Illustrates every part of Smalltalk method syntax. It has unary, binary, and key word messages, declares arguments and 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.”
|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
![Page 5: S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Smalltalk in a Nutshell](https://reader035.vdocuments.us/reader035/viewer/2022062600/5a4d1b537f8b9ab0599a8765/html5/thumbnails/5.jpg)
S.Ducasse 5
Language Constructs^ return “ comments # symbol or array ‘ string [ ] block or byte array .separator and not terminator (or namespace access in VW) ;cascade (sending several messages to the same instance) |local or block variable := assignment $ character :end of selector name e, r number exponent or radix !file element separator <primitive: ...> for VM primitive calls
![Page 6: S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Smalltalk in a Nutshell](https://reader035.vdocuments.us/reader035/viewer/2022062600/5a4d1b537f8b9ab0599a8765/html5/thumbnails/6.jpg)
S.Ducasse 6
Syntax in a Nutshell (I)comment: “a comment”character: $c $h $a $r $a $c $t $e $r $s $# $@ string: ‘a nice string’ ‘lulu’ ‘l’’idiot’symbol: #mac #+ array: #(1 2 3 (1 3) $a 4)byte array: #[1 2 3]integer: 1, 2r101real: 1.5, 6.03e-34,4, 2.4e7float: 1/33boolean: true, falsepoint: 10@120
Note that @ is not an element of the syntax, but just a message sent to a number. This is the same for /, bitShift, ifTrue:, do: ...
![Page 7: S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Smalltalk in a Nutshell](https://reader035.vdocuments.us/reader035/viewer/2022062600/5a4d1b537f8b9ab0599a8765/html5/thumbnails/7.jpg)
S.Ducasse 7
Syntax in a Nutshell (II)assigment: var := aValueblock: [:var ||tmp| expr...]
temporary variable: |tmp|block variable: :varunary message: receiver selectorbinary message: receiver selector argumentkeyword based: receiver keyword1: arg1 keyword2: arg2...cascade: message ; selector ...separator: message . messageresult: ^parenthesis: (...)
![Page 8: S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Smalltalk in a Nutshell](https://reader035.vdocuments.us/reader035/viewer/2022062600/5a4d1b537f8b9ab0599a8765/html5/thumbnails/8.jpg)
S.Ducasse 8
Messages instead of a predefined syntax• In Java, C, C++, Ada constructs like >>, if, for,
etc. are hardcoded into the grammar• In Smalltalk there are just messages defined on
objects• (>>) bitShift: is just a message sent to numbers
– 10 bitShift: 2• (if) ifTrue: is just messages sent to a boolean
– (1> x) ifTrue: • (for) do:, to:do: are just messages to collections
or numbers– #(a b c d) do: [:each | Transcript show: each ; cr]– 1 to: 10 do: [:i | Transcript show: each printString; cr]
• Minimal parsing• Language is extensible
![Page 9: S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Smalltalk in a Nutshell](https://reader035.vdocuments.us/reader035/viewer/2022062600/5a4d1b537f8b9ab0599a8765/html5/thumbnails/9.jpg)
S.Ducasse 9
How to Define a Class (Sq)?• Class Definition: A message sent to
another class
Object subclass: #TomagoshiinstanceVariableNames: ‘tummy hunger dayCount’classVariableNames: ''poolDictionaries: ''category: ‘Monster Inc’
• Instance variables are instance-based protected (not visible by clients)
![Page 10: S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Smalltalk in a Nutshell](https://reader035.vdocuments.us/reader035/viewer/2022062600/5a4d1b537f8b9ab0599a8765/html5/thumbnails/10.jpg)
S.Ducasse 10
Smalltalk defineClass: #Packet superclass: #{Object}
indexedType: #none private: false instanceVariableNames:
'addressee originator contents' classInstanceVariableNames: ''
imports: '' category: 'LAN'
How to Define a Class (VW)?
![Page 11: S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Smalltalk in a Nutshell](https://reader035.vdocuments.us/reader035/viewer/2022062600/5a4d1b537f8b9ab0599a8765/html5/thumbnails/11.jpg)
S.Ducasse 11
How to Define a Method?• Normally defined in a browser or (by
directly invoking the compiler)• Methods are public • Always return self
Tomagoshi>>digest "Digest slowly: every two cycles, remove one
from the tummy”
(dayCount isDivisibleBy: 2)ifTrue: [ tummy := tummy -1]
![Page 12: S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Smalltalk in a Nutshell](https://reader035.vdocuments.us/reader035/viewer/2022062600/5a4d1b537f8b9ab0599a8765/html5/thumbnails/12.jpg)
S.Ducasse 12
How to Define a Method?Normally defined in a browser or (by directly invoking the compiler)Methods are public Always return self
Node>>accept: thePacket "If the packet is addressed to me, print it. Else just behave like a normal node"
(thePacket isAddressedTo: self) ifTrue: [self print: thePacket] ifFalse: [super accept: thePacket]
![Page 13: S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Smalltalk in a Nutshell](https://reader035.vdocuments.us/reader035/viewer/2022062600/5a4d1b537f8b9ab0599a8765/html5/thumbnails/13.jpg)
S.Ducasse 13
Instance Creation1, ‘abc’
Basic class creation messages are new, new:, basicNew, basicNew:Monster new
Class specific message creation(messages sent to classes)
Tomagoshi withHunger: 10
![Page 14: S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Smalltalk in a Nutshell](https://reader035.vdocuments.us/reader035/viewer/2022062600/5a4d1b537f8b9ab0599a8765/html5/thumbnails/14.jpg)
S.Ducasse 14
Messages and their Composition• Three kinds of messages
– Unary: Node new– Binary: 1 + 2, 3@4– Keywords: aTomagoshi eat: #cooky
furiously: true
• Message Priority• (Msg) > unary > binary > keywords• Same Level from left to right
• Example:• (10@0 extent: 10@100) bottomRight• s isNil ifTrue: [ self halt ]
![Page 15: S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Smalltalk in a Nutshell](https://reader035.vdocuments.us/reader035/viewer/2022062600/5a4d1b537f8b9ab0599a8765/html5/thumbnails/15.jpg)
S.Ducasse 15
• Anonymous method• Passed as method argument or stored• Functions
fct(x)= x*x+3, fct(2). fct :=[:x| x * x + 3]. fct value: 2
Integer>>factorial tmp:= 1. 2 to: self do: [:i| tmp := tmp * i]
#(1 2 3) do: [:each | Transcript show: each printString ; cr]
Blocks
![Page 16: S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Smalltalk in a Nutshell](https://reader035.vdocuments.us/reader035/viewer/2022062600/5a4d1b537f8b9ab0599a8765/html5/thumbnails/16.jpg)
S.Ducasse 16
SummaryObjects and MessagesThree kinds of messages
unarybinarykeywords
Block: a.k.a innerclass or closures or lambdaUnary>Binary>Keywords
![Page 17: S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Smalltalk in a Nutshell](https://reader035.vdocuments.us/reader035/viewer/2022062600/5a4d1b537f8b9ab0599a8765/html5/thumbnails/17.jpg)
S.Ducasse 17
Roadmap•Syntax in a Nutshell•OO Model in a Nutshell
![Page 18: S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Smalltalk in a Nutshell](https://reader035.vdocuments.us/reader035/viewer/2022062600/5a4d1b537f8b9ab0599a8765/html5/thumbnails/18.jpg)
S.Ducasse 18
Instance and ClassOnly one modelUniformly appliedClasses are objects too
![Page 19: S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Smalltalk in a Nutshell](https://reader035.vdocuments.us/reader035/viewer/2022062600/5a4d1b537f8b9ab0599a8765/html5/thumbnails/19.jpg)
S.Ducasse 19
Lookup…Class + Inheritance
1
2
![Page 20: S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Smalltalk in a Nutshell](https://reader035.vdocuments.us/reader035/viewer/2022062600/5a4d1b537f8b9ab0599a8765/html5/thumbnails/20.jpg)
S.Ducasse 20
Classes are objects too• Instance creation is just a message
send to a ... Class•Same method lookup than with any
other objects•a Class is the single instance of
amanonymous class• Point is the single instance of Point class
![Page 21: S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Smalltalk in a Nutshell](https://reader035.vdocuments.us/reader035/viewer/2022062600/5a4d1b537f8b9ab0599a8765/html5/thumbnails/21.jpg)
S.Ducasse 21
Parallel Inheritance between classes
![Page 22: S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Smalltalk in a Nutshell](https://reader035.vdocuments.us/reader035/viewer/2022062600/5a4d1b537f8b9ab0599a8765/html5/thumbnails/22.jpg)
S.Ducasse 22
Lookup and Class Methods
1
2
1
2
Workstation withName: ‘BigMac’
aWorkstation name
![Page 23: S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Smalltalk in a Nutshell](https://reader035.vdocuments.us/reader035/viewer/2022062600/5a4d1b537f8b9ab0599a8765/html5/thumbnails/23.jpg)
S.Ducasse 23
About the Buttons
![Page 24: S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Smalltalk in a Nutshell](https://reader035.vdocuments.us/reader035/viewer/2022062600/5a4d1b537f8b9ab0599a8765/html5/thumbnails/24.jpg)
S.Ducasse 24
Summary- Everything is an object- One single model- Single inheritance- Public methods- Object based private attribute- Protected to subclasses- Classes are simply objects too- Class is instance of another class- One unique method lookup look in the class of the receiver