SlideShare a Scribd company logo
S.Ducasse 1
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
Stéphane Ducasse
Stephane.Ducasse@univ-savoie.fr
http://www.iam.unibe.ch/~ducasse/
Smalltalk in a Nutshell
S.Ducasse 2
Goals
• Syntax in a Nutshell
• OO Model in a Nutshell
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 subclasses
Methods are public
Everything is a pointer
Garbage collector
Single inheritance between classes
Only message passing between objects
S.Ducasse 4
Complete Syntax on a PostCard
exampleWithNumber: 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
S.Ducasse 5
Language Constructs
^ return
“ comments
# symbol or array
‘ string
[ ] block or byte array
. separator and not terminator (or namespace access inVW)
; 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: ...> forVM primitive calls
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, 2r101
real: 1.5, 6.03e-34,4, 2.4e7
float: 1/33
boolean: true, false
point: 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: ...
S.Ducasse 7
Syntax in a Nutshell (II)
assigment: var := aValue
block: [:var ||tmp| expr...]
temporary variable: |tmp|
block variable: :var
unary message: receiver selector
binary message: receiver selector argument
keyword based: receiver keyword1: arg1 keyword2:
arg2...
cascade: message ; selector ...
separator: message . message
result: ^
parenthesis: (...)
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
S.Ducasse 9
How to Define a Class (Sq)?
• Class Definition:A message sent to another class
Object subclass: #Tomagoshi
instanceVariableNames: ‘tummy hunger
dayCount’
classVariableNames: ''
poolDictionaries: ''
category: ‘Monster Inc’
• Instance variables are instance-based protected
(not visible by clients)
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)?
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]
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]
S.Ducasse 13
Instance Creation
1,‘abc’
Basic class creation messages are
new, new:,
basicNew, basicNew:
Monster new
Class specific message creation
(messages sent to classes)
Tomagoshi withHunger: 10
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 ]
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
S.Ducasse 16
Summary
Objects and Messages
Three kinds of messages
unary
binary
keywords
Block: a.k.a innerclass or closures or lambda
Unary>Binary>Keywords
S.Ducasse 17
Roadmap
• Syntax in a Nutshell
• OO Model in a Nutshell
S.Ducasse 18
Instance and Class
Only one model
Uniformly applied
Classes are objects too
S.Ducasse 19
Lookup…Class + Inheritance
1
2
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
S.Ducasse 21
Parallel Inheritance between classes
S.Ducasse 22
Lookup and Class Methods
1
2
1
2
Workstation withName: ‘BigMac’
aWorkstation
name
S.Ducasse 23
About the Buttons
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

More Related Content

PPT
03 standardclasses
PPT
12 virtualmachine
PPT
07 bestpractice
PPT
PPT
08 refactoring
PPT
8 - OOP - Smalltalk Syntax
03 standardclasses
12 virtualmachine
07 bestpractice
08 refactoring
8 - OOP - Smalltalk Syntax

What's hot (20)

PPT
4 - OOP - Taste of Smalltalk (Squeak)
PPT
Stoop ed-class forreuse
PPT
5 - OOP - Smalltalk in a Nutshell (b)
PPT
8 - OOP - Syntax & Messages
PPT
Stoop sed-sharing ornot
PPT
PPT
PPT
5 - OOP - Smalltalk in a Nutshell (c)
PPT
09 metaclasses
PPT
4 - OOP - Taste of Smalltalk (Tamagoshi)
PPT
9 - OOP - Smalltalk Classes (a)
PPT
9 - OOP - Smalltalk Classes (b)
PDF
Class notes(week 6) on inheritance and multiple inheritance
PPT
Stoop 423-smalltalk idioms
PPT
Stoop ed-some principles
PPT
Stoop 414-smalltalk elementsofdesign
PDF
Dotnet framework difference faqs- 2
PPT
Inheritance
PPT
Stoop 304-metaclasses
4 - OOP - Taste of Smalltalk (Squeak)
Stoop ed-class forreuse
5 - OOP - Smalltalk in a Nutshell (b)
8 - OOP - Syntax & Messages
Stoop sed-sharing ornot
5 - OOP - Smalltalk in a Nutshell (c)
09 metaclasses
4 - OOP - Taste of Smalltalk (Tamagoshi)
9 - OOP - Smalltalk Classes (a)
9 - OOP - Smalltalk Classes (b)
Class notes(week 6) on inheritance and multiple inheritance
Stoop 423-smalltalk idioms
Stoop ed-some principles
Stoop 414-smalltalk elementsofdesign
Dotnet framework difference faqs- 2
Inheritance
Stoop 304-metaclasses
Ad

Viewers also liked (20)

PPT
PPT
Stoop 440-adaptor
PPT
12 - Conditions and Loops
PPT
10 - OOP - Inheritance (b)
PPT
Stoop 422-naming idioms
PPT
Debugging VisualWorks
PPT
Double Dispatch
PPT
Stoop metaclasses
PPT
Stoop 400 o-metaclassonly
PPT
Stoop 431-visitor
PPT
Stoop 439-decorator
PPT
Stoop 413-abstract classes
PPT
Stoop sed-class initialization
PPT
10 reflection
PPT
PPT
14 - Exceptions
PPT
Stoop ed-frameworks
Stoop 440-adaptor
12 - Conditions and Loops
10 - OOP - Inheritance (b)
Stoop 422-naming idioms
Debugging VisualWorks
Double Dispatch
Stoop metaclasses
Stoop 400 o-metaclassonly
Stoop 431-visitor
Stoop 439-decorator
Stoop 413-abstract classes
Stoop sed-class initialization
10 reflection
14 - Exceptions
Stoop ed-frameworks
Ad

Similar to 5 - OOP - Smalltalk in a Nutshell (a) (20)

PPT
4 - OOP - Taste of Smalltalk (VisualWorks)
PPT
9 - OOP - Smalltalk Classes (c)
PDF
Pharo: Syntax in a Nutshell
PPTX
Smalltalk.pptx
PDF
Introduction to Smalltalk
PDF
Pharo - I have a dream @ Smalltalks Conference 2009
PPT
Stoop 305-reflective programming5
PDF
2013 lecture-02-syntax shortnewcut
PPT
Stoop 300-block optimizationinvw
PPT
10 - OOP - Inheritance (a)
PPT
7 - OOP - OO Concepts
PDF
Introduction to Ruby Programming Language
PDF
Ruby an overall approach
KEY
Pharo, an innovative and open-source Smalltalk
ZIP
Meta Programming in Ruby - Code Camp 2010
PDF
Smalltalk, the dynamic language
DOCX
Ruby Interview Questions
PPT
Stoop 390-instruction stream
PPT
Rubyforjavaprogrammers 1210167973516759-9
PPT
Rubyforjavaprogrammers 1210167973516759-9
4 - OOP - Taste of Smalltalk (VisualWorks)
9 - OOP - Smalltalk Classes (c)
Pharo: Syntax in a Nutshell
Smalltalk.pptx
Introduction to Smalltalk
Pharo - I have a dream @ Smalltalks Conference 2009
Stoop 305-reflective programming5
2013 lecture-02-syntax shortnewcut
Stoop 300-block optimizationinvw
10 - OOP - Inheritance (a)
7 - OOP - OO Concepts
Introduction to Ruby Programming Language
Ruby an overall approach
Pharo, an innovative and open-source Smalltalk
Meta Programming in Ruby - Code Camp 2010
Smalltalk, the dynamic language
Ruby Interview Questions
Stoop 390-instruction stream
Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9

More from The World of Smalltalk (8)

PDF
05 seaside canvas
PPT
Stoop sed-smells
PPT
Stoop sed-class initialization
PPT
Stoop ed-unit ofreuse
PPT
Stoop ed-subtyping subclassing
PPT
Stoop ed-inheritance composition
PPT
Stoop ed-dual interface
PPT
Stoop 450-s unit
05 seaside canvas
Stoop sed-smells
Stoop sed-class initialization
Stoop ed-unit ofreuse
Stoop ed-subtyping subclassing
Stoop ed-inheritance composition
Stoop ed-dual interface
Stoop 450-s unit

Recently uploaded (20)

PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Pre independence Education in Inndia.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Complications of Minimal Access Surgery at WLH
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Pharma ospi slides which help in ospi learning
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Cell Structure & Organelles in detailed.
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Basic Mud Logging Guide for educational purpose
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
human mycosis Human fungal infections are called human mycosis..pptx
Supply Chain Operations Speaking Notes -ICLT Program
Pre independence Education in Inndia.pdf
Anesthesia in Laparoscopic Surgery in India
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Complications of Minimal Access Surgery at WLH
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Pharma ospi slides which help in ospi learning
O5-L3 Freight Transport Ops (International) V1.pdf
O7-L3 Supply Chain Operations - ICLT Program
Cell Structure & Organelles in detailed.
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Basic Mud Logging Guide for educational purpose
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx

5 - OOP - Smalltalk in a Nutshell (a)

  • 1. S.Ducasse 1 QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture. Stéphane Ducasse Stephane.Ducasse@univ-savoie.fr http://www.iam.unibe.ch/~ducasse/ Smalltalk in a Nutshell
  • 2. S.Ducasse 2 Goals • Syntax in a Nutshell • OO Model in a Nutshell
  • 3. 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 subclasses Methods are public Everything is a pointer Garbage collector Single inheritance between classes Only message passing between objects
  • 4. S.Ducasse 4 Complete Syntax on a PostCard exampleWithNumber: 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
  • 5. S.Ducasse 5 Language Constructs ^ return “ comments # symbol or array ‘ string [ ] block or byte array . separator and not terminator (or namespace access inVW) ; 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: ...> forVM primitive calls
  • 6. 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, 2r101 real: 1.5, 6.03e-34,4, 2.4e7 float: 1/33 boolean: true, false point: 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: ...
  • 7. S.Ducasse 7 Syntax in a Nutshell (II) assigment: var := aValue block: [:var ||tmp| expr...] temporary variable: |tmp| block variable: :var unary message: receiver selector binary message: receiver selector argument keyword based: receiver keyword1: arg1 keyword2: arg2... cascade: message ; selector ... separator: message . message result: ^ parenthesis: (...)
  • 8. 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
  • 9. S.Ducasse 9 How to Define a Class (Sq)? • Class Definition:A message sent to another class Object subclass: #Tomagoshi instanceVariableNames: ‘tummy hunger dayCount’ classVariableNames: '' poolDictionaries: '' category: ‘Monster Inc’ • Instance variables are instance-based protected (not visible by clients)
  • 10. 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)?
  • 11. 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]
  • 12. 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]
  • 13. S.Ducasse 13 Instance Creation 1,‘abc’ Basic class creation messages are new, new:, basicNew, basicNew: Monster new Class specific message creation (messages sent to classes) Tomagoshi withHunger: 10
  • 14. 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 ]
  • 15. 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
  • 16. S.Ducasse 16 Summary Objects and Messages Three kinds of messages unary binary keywords Block: a.k.a innerclass or closures or lambda Unary>Binary>Keywords
  • 17. S.Ducasse 17 Roadmap • Syntax in a Nutshell • OO Model in a Nutshell
  • 18. S.Ducasse 18 Instance and Class Only one model Uniformly applied Classes are objects too
  • 19. S.Ducasse 19 Lookup…Class + Inheritance 1 2
  • 20. 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
  • 22. S.Ducasse 22 Lookup and Class Methods 1 2 1 2 Workstation withName: ‘BigMac’ aWorkstation name
  • 24. 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