SlideShare una empresa de Scribd logo
1 de 35
S.Ducasse
Stéphane Ducasse
Stephane.Ducasse@univ-savoie.fr
http://www.listic.univ-savoie.fr/~ducasse/
1
A Taste of Smalltalk
S.Ducasse
License: CC-Attribution-ShareAlike 2.0
http://creativecommons.org/licenses/by-sa/2.0/
2
S.Ducasse 3
Goals
• Two examples:
• ‘hello world’
• A tamagotchi
• To give you an idea of:
• the syntax
• the elementary objects and classes
• the environment
S.Ducasse 4
An Advice
You do not have to know everything!!!
•“Try not to care - Beginning Smalltalk programmers
often have trouble because they think they need to
understand all the details of how a thing works
before they can use it. This means it takes quite a
while before they can master Transcript show: ‘Hello
World’. One of the great leaps in OO is to be able to
answer the question "How does this work?" with "I
don’t care"“. Alan Knight. Smalltalk Guru
• We will show you how to learn and find your way
S.Ducasse 5
Some Conventions
• Return Values
1 + 3 -> 4
Node new -> aNode
• Method selector #add:
• Method scope conventions
• Instance Method defined in class Node:
Node>>accept: aPacket
• Class Method defined in class Node (in the class of
the the class Node)
Node class>>withName: aSymbol
• aSomething is an instance of the class Something
S.Ducasse 6
Roadmap
• “hello world”
• Syntax
• a tamagotchi
S.Ducasse 7
Hello World
• Transcript show: ‘hello world’
• At anytime we can dynamically ask the system to
evaluate an expression. To evaluate an expression,
select it and with the middle mouse button apply
doIt.
• Transcript is a special object that is a kind of
standard output.
• It refers to a TextCollector instance associated with
the launcher.
• In Squeak Transcript is dead slow...
S.Ducasse 8
Transcript show: ‘hello world’
S.Ducasse 9
Everything is an Object
– The workspace is an object.
– The window is an object: it is an instance of SystemWindow.
– The text editor is an object: it is an instance of ParagraphEditor.
– The scrollbars are objects too.
– ‘hello word’ is an object: it is aString instance of String.
– #show: is a Symbol that is also an object.
– The mouse is an object.
– The parser is an object: instance of Parser.
– The compiler is also an object: instance of Compiler.
– The process scheduler is also an object.
– The garbage collector is an object: instance of ObjectMemory.
– Smalltalk is a consistent, uniform world written in itself. You can learn
how it is implemented, you can extend it or even modify it. All the code
is available and readable
S.Ducasse 10
Smalltalk Object Model
• ***Everything*** is an object
Only message passing
Only late binding
• Instance variables are private to the object
• Methods are public
• Everything is a pointer
• Garbage collector
• Single inheritance between classes
• Only message passing between objects
S.Ducasse 11
Roadmap
• Hello World
• First look at the syntax
• a Tamagotchi
S.Ducasse 12
Power & Simplicity: The Syntax on a
PostCard
•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
S.Ducasse 13
Yes ifTrue: is sent to a boolean
Weather isRaining
ifTrue: [self takeMyUmbrella]
ifFalse: [self takeMySunglasses]
ifTrue:ifFalse is sent to an object: a boolean!
S.Ducasse 14
Yes a collection is iterating on itself
•#(1 2 -4 -86)
• do: [:each | Transcript show: each abs
printString ;cr ]
•> 1
•> 2
•> 4
•> 86
•Yes we ask the collection object to perform the
loop on itself
S.Ducasse 15
DoIt, PrintIt, InspectIt and
Accept
• Accept = Compile: Accept a method or a class
definition
• DoIt = send a message to an object
• PrintIt = send a message to an object + print
the result (#printOn:)
• InspectIt = send a message to an object +
inspect the result (#inspect)
S.Ducasse 16
Objects send messages
• Transcript show: ‘hello world’
• The above expression is a message
• the object Transcript is the receiver of the message
• the selector of the message is #show:
• one argument: a string ‘hello world’
• Transcript is a global variable (starts with an
uppercase letter) that refers to the Launcher’s
report part.
S.Ducasse 17
Vocabulary Point
Message passing or sending a message is
equivalent to
invoking a method in Java or C++
calling a procedure in procedural languages
applying a function in functional languages
of course the last two points must be considered
under the light of polymorphism
S.Ducasse 18
Roadmap
• Hello World
• First look at the syntax
• A tamagotchi
S.Ducasse 19
Tamagotchi
• Small entity
– Its own night and day cycle
– Eating, sleeping, been hungry, been satisfied
– Changing color to indicate its mood
S.Ducasse 20
Tomagotchi
woken up
sleeping
satisfied
hungry
fallAsleep
wakeUp
night
isSatisfied
eat
day
isHungry
S.Ducasse 21
Instantiating…
• To create a tomagoshi:
• Tomagoshi newStandAlone openInWorld
S.Ducasse 22
How to Define a Class (Sq)
• Fill the template:
NameOfSuperclass subclass: #NameOfClass
instanceVariableNames: 'instVarName1'
classVariableNames: 'ClassVarName1
ClassVarName2'
poolDictionaries: ''
category: ’category name'
S.Ducasse 23
Tomagoshi (Sq)
• For example to create the class Tomagoshi
Morph subclass: #Tomagoshi
instanceVariableNames: ‘tummy hunger
dayCount isNight'
classVariableNames: ''
poolDictionaries: ''
category: ’TOMA'
S.Ducasse 24
Class Comment!
• I represent a tomagoshi. A small virtual animal that
have its own life.
• dayCount <Number> represents the number of hour
(or tick) in my day and night.
• isNight <Boolean> represents the fact that this is
the night.
• tummy <Number> represents the number of times
you feed me by clicking on me.
• hunger <Number> represents my appetite power.
• I will be hungry if you do not feed me enough, but
I'm selfish so as soon as I' satisfied I fall asleep
because I do not have a lot to say.
S.Ducasse 25
How to define a method?
message selector and argument names
"comment stating purpose of message"
| temporary variable names |
statements
Tomagoshi>>initializeToStandAlone
“Initialize the internal state of a newly created tomagoshi”
super initializeToStandAlone.
tummy := 0.
hunger := 2 atRandom + 1.
self dayStart.
self wakeUp
S.Ducasse 26
Initializing
Tomagoshi>>initializeToStandAlone
“Initialize the internal state of a newly created
tomagoshi”
super initializeToStandAlone.
tummy := 0.
hunger := 2 atRandom + 1.
self dayStart.
self wakeUp
S.Ducasse 27
dayStart
Tomagoshi>>dayStart
night := false.
dayCount := 10
S.Ducasse 28
Step
step
“This method is called by the system at regurlar time interval. It
defines the tomagoshi behavior.”
self timePass.
self isHungry
ifTrue: [self color: Color red].
self isSatisfied
ifTrue:
[self color: Color blue.
self fallAsleep].
self isNight
ifTrue:
[self color: Color black.
self fallAsleep]
S.Ducasse 29
Time Pass
Tomagoshi>>timePass
"Manage the night and day alternance"
Smalltalk beep.
dayCount := dayCount -1.
dayCount isZero
ifTrue:[ self nightOrDayEnd.
dayCount := 10].
self digest
Tomagoshi>>nightOrDayEnd
"alternate night and day"
night := night not
S.Ducasse 30
Digest
Tomagoshi>>digest
"Digest slowly: every two cycle, remove one from the
tummy”
(dayCount isDivisibleBy: 2)
ifTrue: [ tummy := tummy -1]
S.Ducasse 31
Testing
Tomagoshi>>isHungry
^ hunger > tummy
Tomagoshi>>isSatisfied
^self isHungry not
Tomagoshi>>isNight
^ night
S.Ducasse 32
State
Tomagoshi>>wakeUp
self color: Color green.
state := self wakeUpState
Tomagoshi>>wakeUpState
"Return how we codify the fact that I sleep"
^ #sleep
Tomagoshi>> isSleeping
^ state = self wakeUpState
S.Ducasse 33
Eating
Tomagoshi>>eat
tummy := tummy + 1
S.Ducasse 34
Time and Events
Tomagoshi>>stepTime
"The step method is executed every steppingTime
ms"
^ 500
Tomagoshi>>handlesMouseDown: evt
"true means that the morph can react when the mouse
down over it"
^ true
Tomagoshi>>mouseDown: evt
self eat
S.Ducasse 35
Summary
What is a message?
What is the message receiver?
What is the method selector?
How to create a class?
How to define a method?

Más contenido relacionado

La actualidad más candente

4 - OOP - Taste of Smalltalk (VisualWorks)
4 - OOP - Taste of Smalltalk (VisualWorks)4 - OOP - Taste of Smalltalk (VisualWorks)
4 - OOP - Taste of Smalltalk (VisualWorks)
The World of Smalltalk
 

La actualidad más candente (20)

Stoop 416-lsp
Stoop 416-lspStoop 416-lsp
Stoop 416-lsp
 
04 idioms
04 idioms04 idioms
04 idioms
 
9 - OOP - Smalltalk Classes (b)
9 - OOP - Smalltalk Classes (b)9 - OOP - Smalltalk Classes (b)
9 - OOP - Smalltalk Classes (b)
 
07 bestpractice
07 bestpractice07 bestpractice
07 bestpractice
 
4 - OOP - Taste of Smalltalk (VisualWorks)
4 - OOP - Taste of Smalltalk (VisualWorks)4 - OOP - Taste of Smalltalk (VisualWorks)
4 - OOP - Taste of Smalltalk (VisualWorks)
 
5 - OOP - Smalltalk in a Nutshell (a)
5 - OOP - Smalltalk in a Nutshell (a)5 - OOP - Smalltalk in a Nutshell (a)
5 - OOP - Smalltalk in a Nutshell (a)
 
5 - OOP - Smalltalk in a Nutshell (b)
5 - OOP - Smalltalk in a Nutshell (b)5 - OOP - Smalltalk in a Nutshell (b)
5 - OOP - Smalltalk in a Nutshell (b)
 
2 - OOP
2 - OOP2 - OOP
2 - OOP
 
02 basics
02 basics02 basics
02 basics
 
Debugging VisualWorks
Debugging VisualWorksDebugging VisualWorks
Debugging VisualWorks
 
12 virtualmachine
12 virtualmachine12 virtualmachine
12 virtualmachine
 
11 bytecode
11 bytecode11 bytecode
11 bytecode
 
8 - OOP - Syntax & Messages
8 - OOP - Syntax & Messages8 - OOP - Syntax & Messages
8 - OOP - Syntax & Messages
 
Stoop ed-subtyping subclassing
Stoop ed-subtyping subclassingStoop ed-subtyping subclassing
Stoop ed-subtyping subclassing
 
Stoop 303-advanced blocks
Stoop 303-advanced blocksStoop 303-advanced blocks
Stoop 303-advanced blocks
 
03 standardclasses
03 standardclasses03 standardclasses
03 standardclasses
 
06 debugging
06 debugging06 debugging
06 debugging
 
durability, durability, durability
durability, durability, durabilitydurability, durability, durability
durability, durability, durability
 
Stop that!
Stop that!Stop that!
Stop that!
 
Cassandra, Modeling and Availability at AMUG
Cassandra, Modeling and Availability at AMUGCassandra, Modeling and Availability at AMUG
Cassandra, Modeling and Availability at AMUG
 

Similar a 4 - OOP - Taste of Smalltalk (Tamagoshi)

Similar a 4 - OOP - Taste of Smalltalk (Tamagoshi) (20)

Choose'10: Stephane Ducasse - Powerful DSL engineering in Smalltalk
Choose'10: Stephane Ducasse - Powerful DSL engineering in SmalltalkChoose'10: Stephane Ducasse - Powerful DSL engineering in Smalltalk
Choose'10: Stephane Ducasse - Powerful DSL engineering in Smalltalk
 
Stoop 423-some designpatterns
Stoop 423-some designpatternsStoop 423-some designpatterns
Stoop 423-some designpatterns
 
Stoop 304-metaclasses
Stoop 304-metaclassesStoop 304-metaclasses
Stoop 304-metaclasses
 
7 - OOP - OO Concepts
7 - OOP - OO Concepts7 - OOP - OO Concepts
7 - OOP - OO Concepts
 
1 - OOP
1 - OOP1 - OOP
1 - OOP
 
10 - OOP - Inheritance (a)
10 - OOP - Inheritance (a)10 - OOP - Inheritance (a)
10 - OOP - Inheritance (a)
 
Stoop 413-abstract classes
Stoop 413-abstract classesStoop 413-abstract classes
Stoop 413-abstract classes
 
Scalable JavaScript Design Patterns
Scalable JavaScript Design PatternsScalable JavaScript Design Patterns
Scalable JavaScript Design Patterns
 
10 - OOP - Inheritance (b)
10 - OOP - Inheritance (b)10 - OOP - Inheritance (b)
10 - OOP - Inheritance (b)
 
Stoop sed-smells
Stoop sed-smellsStoop sed-smells
Stoop sed-smells
 
9 - OOP - Smalltalk Classes (a)
9 - OOP - Smalltalk Classes (a)9 - OOP - Smalltalk Classes (a)
9 - OOP - Smalltalk Classes (a)
 
Stoop 450-s unit
Stoop 450-s unitStoop 450-s unit
Stoop 450-s unit
 
Stoop 422-naming idioms
Stoop 422-naming idiomsStoop 422-naming idioms
Stoop 422-naming idioms
 
5 - OOP - Smalltalk in a Nutshell (c)
5 - OOP - Smalltalk in a Nutshell (c)5 - OOP - Smalltalk in a Nutshell (c)
5 - OOP - Smalltalk in a Nutshell (c)
 
Pharo - I have a dream @ Smalltalks Conference 2009
Pharo -  I have a dream @ Smalltalks Conference 2009Pharo -  I have a dream @ Smalltalks Conference 2009
Pharo - I have a dream @ Smalltalks Conference 2009
 
Class 9 Lecture Notes
Class 9 Lecture NotesClass 9 Lecture Notes
Class 9 Lecture Notes
 
9 - OOP - Smalltalk Classes (c)
9 - OOP - Smalltalk Classes (c)9 - OOP - Smalltalk Classes (c)
9 - OOP - Smalltalk Classes (c)
 
Pg py-and-squid-pypgday
Pg py-and-squid-pypgdayPg py-and-squid-pypgday
Pg py-and-squid-pypgday
 
Stoop ed-lod
Stoop ed-lodStoop ed-lod
Stoop ed-lod
 
Stoop sed-class initialization
Stoop sed-class initializationStoop sed-class initialization
Stoop sed-class initialization
 

Más de The World of Smalltalk (14)

05 seaside canvas
05 seaside canvas05 seaside canvas
05 seaside canvas
 
99 questions
99 questions99 questions
99 questions
 
13 traits
13 traits13 traits
13 traits
 
10 reflection
10 reflection10 reflection
10 reflection
 
09 metaclasses
09 metaclasses09 metaclasses
09 metaclasses
 
08 refactoring
08 refactoring08 refactoring
08 refactoring
 
05 seaside
05 seaside05 seaside
05 seaside
 
01 intro
01 intro01 intro
01 intro
 
Stoop metaclasses
Stoop metaclassesStoop metaclasses
Stoop metaclasses
 
Stoop ed-unit ofreuse
Stoop ed-unit ofreuseStoop ed-unit ofreuse
Stoop ed-unit ofreuse
 
Stoop ed-inheritance composition
Stoop ed-inheritance compositionStoop ed-inheritance composition
Stoop ed-inheritance composition
 
Stoop ed-frameworks
Stoop ed-frameworksStoop ed-frameworks
Stoop ed-frameworks
 
Stoop ed-dual interface
Stoop ed-dual interfaceStoop ed-dual interface
Stoop ed-dual interface
 
Stoop 440-adaptor
Stoop 440-adaptorStoop 440-adaptor
Stoop 440-adaptor
 

Último

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 

Último (20)

Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 

4 - OOP - Taste of Smalltalk (Tamagoshi)

  • 3. S.Ducasse 3 Goals • Two examples: • ‘hello world’ • A tamagotchi • To give you an idea of: • the syntax • the elementary objects and classes • the environment
  • 4. S.Ducasse 4 An Advice You do not have to know everything!!! •“Try not to care - Beginning Smalltalk programmers often have trouble because they think they need to understand all the details of how a thing works before they can use it. This means it takes quite a while before they can master Transcript show: ‘Hello World’. One of the great leaps in OO is to be able to answer the question "How does this work?" with "I don’t care"“. Alan Knight. Smalltalk Guru • We will show you how to learn and find your way
  • 5. S.Ducasse 5 Some Conventions • Return Values 1 + 3 -> 4 Node new -> aNode • Method selector #add: • Method scope conventions • Instance Method defined in class Node: Node>>accept: aPacket • Class Method defined in class Node (in the class of the the class Node) Node class>>withName: aSymbol • aSomething is an instance of the class Something
  • 6. S.Ducasse 6 Roadmap • “hello world” • Syntax • a tamagotchi
  • 7. S.Ducasse 7 Hello World • Transcript show: ‘hello world’ • At anytime we can dynamically ask the system to evaluate an expression. To evaluate an expression, select it and with the middle mouse button apply doIt. • Transcript is a special object that is a kind of standard output. • It refers to a TextCollector instance associated with the launcher. • In Squeak Transcript is dead slow...
  • 8. S.Ducasse 8 Transcript show: ‘hello world’
  • 9. S.Ducasse 9 Everything is an Object – The workspace is an object. – The window is an object: it is an instance of SystemWindow. – The text editor is an object: it is an instance of ParagraphEditor. – The scrollbars are objects too. – ‘hello word’ is an object: it is aString instance of String. – #show: is a Symbol that is also an object. – The mouse is an object. – The parser is an object: instance of Parser. – The compiler is also an object: instance of Compiler. – The process scheduler is also an object. – The garbage collector is an object: instance of ObjectMemory. – Smalltalk is a consistent, uniform world written in itself. You can learn how it is implemented, you can extend it or even modify it. All the code is available and readable
  • 10. S.Ducasse 10 Smalltalk Object Model • ***Everything*** is an object Only message passing Only late binding • Instance variables are private to the object • Methods are public • Everything is a pointer • Garbage collector • Single inheritance between classes • Only message passing between objects
  • 11. S.Ducasse 11 Roadmap • Hello World • First look at the syntax • a Tamagotchi
  • 12. S.Ducasse 12 Power & Simplicity: The Syntax on a PostCard •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
  • 13. S.Ducasse 13 Yes ifTrue: is sent to a boolean Weather isRaining ifTrue: [self takeMyUmbrella] ifFalse: [self takeMySunglasses] ifTrue:ifFalse is sent to an object: a boolean!
  • 14. S.Ducasse 14 Yes a collection is iterating on itself •#(1 2 -4 -86) • do: [:each | Transcript show: each abs printString ;cr ] •> 1 •> 2 •> 4 •> 86 •Yes we ask the collection object to perform the loop on itself
  • 15. S.Ducasse 15 DoIt, PrintIt, InspectIt and Accept • Accept = Compile: Accept a method or a class definition • DoIt = send a message to an object • PrintIt = send a message to an object + print the result (#printOn:) • InspectIt = send a message to an object + inspect the result (#inspect)
  • 16. S.Ducasse 16 Objects send messages • Transcript show: ‘hello world’ • The above expression is a message • the object Transcript is the receiver of the message • the selector of the message is #show: • one argument: a string ‘hello world’ • Transcript is a global variable (starts with an uppercase letter) that refers to the Launcher’s report part.
  • 17. S.Ducasse 17 Vocabulary Point Message passing or sending a message is equivalent to invoking a method in Java or C++ calling a procedure in procedural languages applying a function in functional languages of course the last two points must be considered under the light of polymorphism
  • 18. S.Ducasse 18 Roadmap • Hello World • First look at the syntax • A tamagotchi
  • 19. S.Ducasse 19 Tamagotchi • Small entity – Its own night and day cycle – Eating, sleeping, been hungry, been satisfied – Changing color to indicate its mood
  • 21. S.Ducasse 21 Instantiating… • To create a tomagoshi: • Tomagoshi newStandAlone openInWorld
  • 22. S.Ducasse 22 How to Define a Class (Sq) • Fill the template: NameOfSuperclass subclass: #NameOfClass instanceVariableNames: 'instVarName1' classVariableNames: 'ClassVarName1 ClassVarName2' poolDictionaries: '' category: ’category name'
  • 23. S.Ducasse 23 Tomagoshi (Sq) • For example to create the class Tomagoshi Morph subclass: #Tomagoshi instanceVariableNames: ‘tummy hunger dayCount isNight' classVariableNames: '' poolDictionaries: '' category: ’TOMA'
  • 24. S.Ducasse 24 Class Comment! • I represent a tomagoshi. A small virtual animal that have its own life. • dayCount <Number> represents the number of hour (or tick) in my day and night. • isNight <Boolean> represents the fact that this is the night. • tummy <Number> represents the number of times you feed me by clicking on me. • hunger <Number> represents my appetite power. • I will be hungry if you do not feed me enough, but I'm selfish so as soon as I' satisfied I fall asleep because I do not have a lot to say.
  • 25. S.Ducasse 25 How to define a method? message selector and argument names "comment stating purpose of message" | temporary variable names | statements Tomagoshi>>initializeToStandAlone “Initialize the internal state of a newly created tomagoshi” super initializeToStandAlone. tummy := 0. hunger := 2 atRandom + 1. self dayStart. self wakeUp
  • 26. S.Ducasse 26 Initializing Tomagoshi>>initializeToStandAlone “Initialize the internal state of a newly created tomagoshi” super initializeToStandAlone. tummy := 0. hunger := 2 atRandom + 1. self dayStart. self wakeUp
  • 28. S.Ducasse 28 Step step “This method is called by the system at regurlar time interval. It defines the tomagoshi behavior.” self timePass. self isHungry ifTrue: [self color: Color red]. self isSatisfied ifTrue: [self color: Color blue. self fallAsleep]. self isNight ifTrue: [self color: Color black. self fallAsleep]
  • 29. S.Ducasse 29 Time Pass Tomagoshi>>timePass "Manage the night and day alternance" Smalltalk beep. dayCount := dayCount -1. dayCount isZero ifTrue:[ self nightOrDayEnd. dayCount := 10]. self digest Tomagoshi>>nightOrDayEnd "alternate night and day" night := night not
  • 30. S.Ducasse 30 Digest Tomagoshi>>digest "Digest slowly: every two cycle, remove one from the tummy” (dayCount isDivisibleBy: 2) ifTrue: [ tummy := tummy -1]
  • 31. S.Ducasse 31 Testing Tomagoshi>>isHungry ^ hunger > tummy Tomagoshi>>isSatisfied ^self isHungry not Tomagoshi>>isNight ^ night
  • 32. S.Ducasse 32 State Tomagoshi>>wakeUp self color: Color green. state := self wakeUpState Tomagoshi>>wakeUpState "Return how we codify the fact that I sleep" ^ #sleep Tomagoshi>> isSleeping ^ state = self wakeUpState
  • 34. S.Ducasse 34 Time and Events Tomagoshi>>stepTime "The step method is executed every steppingTime ms" ^ 500 Tomagoshi>>handlesMouseDown: evt "true means that the morph can react when the mouse down over it" ^ true Tomagoshi>>mouseDown: evt self eat
  • 35. S.Ducasse 35 Summary What is a message? What is the message receiver? What is the method selector? How to create a class? How to define a method?