SlideShare a Scribd company logo
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Lecture 3 :

Introduction to Smalltalk
LSINF 2335
Programming Paradigms
Prof. Kim Mens
UCL / EPL / INGI
(Slides revised with the help of

Dr. Johan Brichau and partially based

on slides by Kris Gybels and others)
Disclaimer: any pictures used in this presentation
remain with the copyright of their original owner and
were included here for didactical purposes only. In case
of any suspected violation of copyright please contact
me so that I can remove or replace those pictures.
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Smalltalk in a nutshell (1)
■ Pure object-oriented language :
– Everything is an object
– Objects communicate only by message passing
■ Class-based language :
– Every object is an instance of a class
– All classes have a (single) parent class
– Object is the root class
■ Uniform & simple syntax
– Forces one to think object-oriented
– Syntax fits on a single sheet of paper
– Statements read almost like English phrases
– Originally designed as a learning environment for children
■ Dynamically typed
■ Fully reflective language
– IDE entirely written (and modifiable) in Smalltalk itself
2
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Smalltalk in a nutshell (2)
■ Platform independent
– A runtime environment based on VM technology
■ Encourages exploratory programming
■ Memory management (garbage collection)
■ A large set of reusable classes
– basic data structures, GUI classes, DB access, internet, graphics, …
■ Powerful development tools
– browsers, GUI builders, inspectors, change management tools, crash
recovery tools, project management tools, unit testing, refactoring, …
■ Team working environment
– releasing, versioning, deploying
■ Source of inspiration to many other languages and environments
– environment, collections, debugger, reflective features

and many development tools 3
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
A byte-code interpreter:
the virtual machine interpretes the image
IMAGE2.IM
IMAGE2.CHA
Standard SOURCES
Shared by everybody
IMAGE1.IM
IMAGE1.CHA
All the objects of the system
at a moment in time
One per user
+
Smalltalk run-time architecture
■ Virtual Machine + Image + Changes and Sources
■ Image = bytecode
■ Sources and changes = code (text)
4
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Point
x, y
Classes
Concepts of Smalltalk: `pure’ OO
5
a Point
x
y
a SmallInteger
5
a SmallInteger
10
Objects
add:
a SmallInteger
15Messages
add: extra
x := x + extra.
y := y + extra.
Methods
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Everything is an object
6
a Person
name
address
a ByteString
‘Kim Mens’
an Address
street
nr
a ByteString
‘Pl. Sainte Barbe’
a SmallInteger
2
Objects have state
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Everything is an object
■ Workspaces, windows, text editors, scrollbars, ... are all objects.
■ ‘hello word’ is an object: it is an instance of String.
■ #show: is a Symbol that is also an object.
■ Numbers are instances of classes: 1 is instance of Integer
■ Booleans are instances of their class: true is instance of class True
■ The parser, compiler and garbage collector are objects.
■ Even classes are objects themselves
■ 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.
– Smalltalk is a fully reflective language
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Objects communicate using messages
8
a SmallInteger
15
add:
Objects can be

sent messages ...
add: extra
x := x + extra.
y := y + extra.
... and reply by
executing a method
↺
a SmallInteger
20
+
a SmallInteger
5
a SmallInteger
10
a Point
x
y
a SmallInteger
25
+
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Objects communicate using messages
■ A message is a request to an object to do something
■ A method is piece of source code belonging to a class
■ Transcript show: ‘hello world’
■ The above expression is a message
– Transcript is a global variable containing a Stream object
that represents a “console” window.
– show: ‘hello world’ is a message sent to a Stream object
• The stream object (value of Transcript) is the receiver of the
message
• #show: is the selector of the message
• the string ‘hello world’ is the (actual) argument of the message
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Objects communicate using messages
■ An object executes a method when it receives a message.
■ ‘hello world’ reverse
– Object ‘hello world’ receives a request to reverse itself
– Method reverse is defined on the class String
– Executing this method returns a new string ‘dlrow olleh’
■ Transcript show: ‘hello world’
– Object Transcript receives a request to show a string and results
in the string being printed on the transcript window.
■ Message passing or sending a message is similar to
– invoking a method in Java or C++
– calling a procedure in procedural languages
– applying a function in functional languages
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Try it...
11
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Smalltalk basic expressions
12
Literals
Variable Reference
Message
Expressions
Return
Expressions
Block Expressions
1234
someObject
someObject doSomethingWith: 1234
^ someObject print: ‘Johan’
$a #Johan ‘Kim’ #(#a #b)
a GlobalVariable
[ :x :y | x + y ]
1 + 2 ‘Kim’ , ‘Mens’ #(#a #b) first
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Literal Expressions
13
a ByteString
‘Pleinlaan’
a
42
a ByteSymbol
#Brussels
a Character
$a
‘Pleinlaan’↺
= = = =
42
↺
#Brussels
↺
$a
↺
#(1 #a $c)
↺
an Array
1
2
3
= a SmallInteger
1
a Character
$c
a
ByteSymbol
#a
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Three kinds of messages
14
a
SomeObjectsomeVar
+ add: with:
a Number
42
a Number
100
printYourself
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Unary message
15
printString
an Assistant
name
a ByteString
‘Johan Brichau’
=
a ByteString
‘Brichau J.’
selector is alphabetic name

no arguments
selector is printString
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Unary message expressions
16
42 cos 42 cos sin
↺ =
cos a SmallInteger
42
=
a Float
-0.399985
cos a SmallInteger
42
a Float
-0.399985
↺
sin
=
a Float
-0.389405
=
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Binary messages
17
a SmallInteger
10
+
a SmallInteger
42
selector is one or two special characters

exactly one argument (and one receiver)
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Binary message expressions
18
1 + 2 cos 1 + (2 cos)=
1 + 2 cos - 3 (1 + (2 cos)) - 3=
1 + 2 cos + 3 (1 + (2 cos)) +3=
1 + 2 * 3 + 4 ((1 + 2) * 3) + 4=
1 + 2 * 3 + 4 1 + (2 * 3) + 4=!
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
a ByteString
Boo’
Keyword messages
19
a
SomeObjectsomeVar
add: with: andPrint:
a
SmallInteger42
a
SmallInteger100
Selector is sequence of names ending in colon (a keyword)

One argument for each keyword
selector is
add:with:andPrint:
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Keyword message expressions
20
Time readFrom: (ReadStream on: ‘1:30:00 pm’)
Time readFrom: ReadStream on: ‘1:30:00 pm’
=
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
■Unary message 

>> binary message 

>> keyword message
■ Evaluation from left to right
■ There are no specific precedence rules for the
arithmetic or logic operators
2 negated raisedTo: 3 + 2 * 5
is equivalent with
(2 negated) raisedTo: ((3 + 2) * 5)
Precedence rules
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
■ In Java different predefined syntactic constructs like >>, if, for,
while, … are hard-coded into the language grammar
■ In Smalltalk, they are just defined on objects, e.g. :
(>>) 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: [:each | Transcript show: each printString ]
■ Advantages :
– Language is extensible (very easy to add “new syntax”)
– Simplicity & orthogonality (uniform language design)
Control structures
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Blocks
23
[ :foo :bar |foo + bar ]
↺
a BlockClosure
=
value: value:
Blocks are “delayed” expressions
Blocks can be sent messages to evaluate
the body of the block
[ :foo :bar |foo + bar ] value: 4 value: 2
↺
=
a SmallInteger
6
a BlockClosure
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Block expressions
24
[ Transcript show: 'Boo!'] value
ablock := [ :someName | someName asUppercase ].
ablock value: 'Kim'
[ :a :b| a + b ] valueWithArguments: (Array with: 1 with: 2)
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Blocks example: if then else
25
a TrueifTrue: ifFalse:
a BlockClosure a BlockClosure
value
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Blocks example: if then else
26
a FalseifTrue: ifFalse:
a BlockClosure a BlockClosure
value
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Blocks
27
true ifTrue: [ Transcript show: ‘yes’ ] ifFalse: [ Transcript show: ‘no’ ]
(a < b ) ifTrue: [ Transcript show: ‘a smaller’ ]
ifFalse: [ Transcript show: ‘a not smaller’ ]
Transcript show: ((a < b) ifTrue: [ ‘a’ ] ifFalse: [‘a not’ ]) , ‘ smaller’
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
■ A method that illustrates every part of Smalltalk method syntax
except primitives.
uselessMethod: x
|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
Power & Simplicity: Syntax on a PostCard
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Power & Simplicity: Syntax on a PostCard
■ This (useless) method illustrates the entire Smalltalk syntax
– messages
• unary, binary, and key word messages
• cascaded messages
– variables
• arguments and temporaries (local variables) ; no block temporaries
• global variables
• accessing and assigning a variable
• pseudo variables (true, false, nil, self, super)
– instructions
• sequence of instructions (.)
• return statement (^)
– blocks (both zero argument and one argument block)
– literals (array, character, symbol, string, integer, float)
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
“a comment”
$c $t $e $r $# $@
‘a string’ ‘lulu’ ‘l’’idiot’
#mac #+
#(1 2 3 (1 3) $a 4)
1, 2r101
1.5, 6.03e-34,4, 2.4e7
1/33
true, false
10@120
<primitive: …>
( … )
Smalltalk Syntax (1)
■ comment
■ character
■ string
■ symbol
■ array
■ integer (r = radix)
■ real (e = exponent)
■ fraction
■ boolean
■ point
■ VM primitive call
■ parenthesis (precedence)
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
var := aValue
[:var | |tmp| expr...]
|tmp|
:var
receiver selector
receiver selector arg
receiver kw1: arg1 kw2: arg2
message ; selector; …
message . message
^ message
Smalltalk Syntax (2)
■ assignment
■ a block
■ local variable (method / block)
■ block variable
■ unary message
■ binary message
■ keyword message
■ cascading of messages
= sending several messages to
the same instance
■ sequence of instructions
■ return statement
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Smalltalk versus Java
Smalltalk Java
Syntax Simple Complex
Control structures Defined by the
programmer
Predefined
Typing Dynamic Static (primitive
types, type casts)
Reflection Full reflection Introspection only
(see but not modify)
Garbage collection Yes Yes
Virtual machine Yes Yes
Concurrency Processes Threads
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Smalltalk versus Java
Smalltalk Java
Visibility of variables Protected Public, protected,
private
Visibility of methods Public Public, protected,
private
Dynamicity of methods Dynamic Dynamic or static
Interfaces No Yes
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Smalltalk versus Java
■ Java and C++ have a (too) complex syntax
– The constructs for expressing conditionnals, iteration, arithmetic
and so on are predefined in the syntax
– Conditionnals
• if (BooleanExpression) { /* do something */ }
• if (BooleanExpression) { /* do something */ }

else { /* something else */ }
• case ...
– Iteration
• for (i=1; i<length; i++) { /* do something */ }
• while (BooleanExpression) { /* do something */ }
– Arithmetic
• 2 + 4
• 3 * 7
• 10 << 2 (bitshift)
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Smalltalk versus Java
■ Smalltalk has a much more simple and uniform syntax
– Everything is a message, even
• Conditionnals
• Boolean operators
• Arithmetic operators
• Iteration
– The language is extensible
• Define your own language constructs
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Homework and exercises
36
■ Download and install Pharo on your computer
– Pharo can be downloaded on http://pharo-project.org
– Download the latest stable version
■ Download and read the free online book on Pharo
– “Pharo by Example” can be downloaded on

http://pharobyexample.org
– Read and try chapters 1 to 6 of the book
– (attention: the book may not have been updated yet to the most
recent version of Pharo)
■ Also try out the examples from this theory session.

More Related Content

What's hot (20)

PPTX
Python Functions
Sampad Kar
 
PPTX
Advance OOP concepts in Python
Sujith Kumar
 
PPTX
Regular expressions in Python
Sujith Kumar
 
PPTX
Constructors and destructors
Vineeta Garg
 
PPTX
Stacks in c++
Vineeta Garg
 
PPS
Interface
kamal kotecha
 
PDF
Arrays in java
TharuniDiddekunta
 
PPTX
Polymorphism in C++
Rabin BK
 
PPTX
Python
SHIVAM VERMA
 
PDF
Object Oriented Paradigm
Hüseyin Ergin
 
PDF
Java Inheritance
Rosie Jane Enomar
 
PPTX
Automata theory - Push Down Automata (PDA)
Akila Krishnamoorthy
 
PPT
IIS
Giritharan V
 
PPTX
PYTHON FEATURES.pptx
MaheShiva
 
PDF
08 subprograms
baran19901990
 
PPTX
Dfs presentation
Alizay Khan
 
PPTX
Insertion and Deletion in Binary Search Trees (using Arrays and Linked Lists)
Lovelyn Rose
 
PPT
Major Java 8 features
Sanjoy Kumar Roy
 
Python Functions
Sampad Kar
 
Advance OOP concepts in Python
Sujith Kumar
 
Regular expressions in Python
Sujith Kumar
 
Constructors and destructors
Vineeta Garg
 
Stacks in c++
Vineeta Garg
 
Interface
kamal kotecha
 
Arrays in java
TharuniDiddekunta
 
Polymorphism in C++
Rabin BK
 
Python
SHIVAM VERMA
 
Object Oriented Paradigm
Hüseyin Ergin
 
Java Inheritance
Rosie Jane Enomar
 
Automata theory - Push Down Automata (PDA)
Akila Krishnamoorthy
 
PYTHON FEATURES.pptx
MaheShiva
 
08 subprograms
baran19901990
 
Dfs presentation
Alizay Khan
 
Insertion and Deletion in Binary Search Trees (using Arrays and Linked Lists)
Lovelyn Rose
 
Major Java 8 features
Sanjoy Kumar Roy
 

Viewers also liked (6)

PDF
Smalltalk, the dynamic language
mohamedsamyali
 
PPTX
Basic c++ programs
harman kaur
 
PPT
C++ programming
viancagerone
 
PDF
Introduction to Java Programming Language
jaimefrozr
 
PPT
01 c++ Intro.ppt
Tareq Hasan
 
PPTX
C++ ppt
parpan34
 
Smalltalk, the dynamic language
mohamedsamyali
 
Basic c++ programs
harman kaur
 
C++ programming
viancagerone
 
Introduction to Java Programming Language
jaimefrozr
 
01 c++ Intro.ppt
Tareq Hasan
 
C++ ppt
parpan34
 
Ad

Similar to Introduction to Smalltalk (20)

PPTX
Smalltalk.pptx
McStephenAlumbro2
 
PPT
4 - OOP - Taste of Smalltalk (VisualWorks)
The World of Smalltalk
 
PPT
4 - OOP - Taste of Smalltalk (Squeak)
The World of Smalltalk
 
PPT
5 - OOP - Smalltalk in a Nutshell (b)
The World of Smalltalk
 
PPT
8 - OOP - Syntax & Messages
The World of Smalltalk
 
PPT
5 - OOP - Smalltalk in a Nutshell (a)
The World of Smalltalk
 
PDF
Introduction to Ruby
kim.mens
 
PDF
IronSmalltalk
ESUG
 
PPT
03 standardclasses
The World of Smalltalk
 
PDF
Pharo: Syntax in a Nutshell
Marcus Denker
 
PPT
5 - OOP - Smalltalk in a Nutshell (c)
The World of Smalltalk
 
PDF
Performance from Aligning Smalltalk & Javascript Classes
ESUG
 
PPT
99 questions
The World of Smalltalk
 
PDF
Smalltalk blocks and closures origin and evolution by Juan Escalada
FAST
 
PPTX
LISP: назад в будущее, Микола Мозговий
Sigma Software
 
PPTX
Lecture 2 lisp-Overview
Student at University Of Malakand, Pakistan
 
PDF
Smalltalk In a Nutshell
Michele Lanza
 
PPT
Smalltalk in a .NET World
ESUG
 
Smalltalk.pptx
McStephenAlumbro2
 
4 - OOP - Taste of Smalltalk (VisualWorks)
The World of Smalltalk
 
4 - OOP - Taste of Smalltalk (Squeak)
The World of Smalltalk
 
5 - OOP - Smalltalk in a Nutshell (b)
The World of Smalltalk
 
8 - OOP - Syntax & Messages
The World of Smalltalk
 
5 - OOP - Smalltalk in a Nutshell (a)
The World of Smalltalk
 
Introduction to Ruby
kim.mens
 
IronSmalltalk
ESUG
 
03 standardclasses
The World of Smalltalk
 
Pharo: Syntax in a Nutshell
Marcus Denker
 
5 - OOP - Smalltalk in a Nutshell (c)
The World of Smalltalk
 
Performance from Aligning Smalltalk & Javascript Classes
ESUG
 
Smalltalk blocks and closures origin and evolution by Juan Escalada
FAST
 
LISP: назад в будущее, Микола Мозговий
Sigma Software
 
Smalltalk In a Nutshell
Michele Lanza
 
Smalltalk in a .NET World
ESUG
 
Ad

More from kim.mens (20)

PDF
Software Maintenance and Evolution
kim.mens
 
PDF
Context-Oriented Programming
kim.mens
 
PDF
Software Reuse and Object-Oriented Programming
kim.mens
 
PDF
Bad Code Smells
kim.mens
 
PDF
Object-Oriented Design Heuristics
kim.mens
 
PDF
Software Patterns
kim.mens
 
PDF
Code Refactoring
kim.mens
 
PDF
Domain Modelling
kim.mens
 
PDF
Object-Oriented Application Frameworks
kim.mens
 
PDF
Towards a Context-Oriented Software Implementation Framework
kim.mens
 
PDF
Towards a Taxonomy of Context-Aware Software Variabilty Approaches
kim.mens
 
PDF
Breaking the Walls: A Unified Vision on Context-Oriented Software Engineering
kim.mens
 
PDF
Context-oriented programming
kim.mens
 
PDF
Basics of reflection
kim.mens
 
PDF
Advanced Reflection in Java
kim.mens
 
PDF
Basics of reflection in java
kim.mens
 
PDF
Reflection in Ruby
kim.mens
 
PDF
A gentle introduction to reflection
kim.mens
 
PDF
Managing the Evolution of Information Systems with Intensional Views and Rela...
kim.mens
 
PDF
Usage contracts (presented at SATToSE 2014 in L'Aquila, Italy)
kim.mens
 
Software Maintenance and Evolution
kim.mens
 
Context-Oriented Programming
kim.mens
 
Software Reuse and Object-Oriented Programming
kim.mens
 
Bad Code Smells
kim.mens
 
Object-Oriented Design Heuristics
kim.mens
 
Software Patterns
kim.mens
 
Code Refactoring
kim.mens
 
Domain Modelling
kim.mens
 
Object-Oriented Application Frameworks
kim.mens
 
Towards a Context-Oriented Software Implementation Framework
kim.mens
 
Towards a Taxonomy of Context-Aware Software Variabilty Approaches
kim.mens
 
Breaking the Walls: A Unified Vision on Context-Oriented Software Engineering
kim.mens
 
Context-oriented programming
kim.mens
 
Basics of reflection
kim.mens
 
Advanced Reflection in Java
kim.mens
 
Basics of reflection in java
kim.mens
 
Reflection in Ruby
kim.mens
 
A gentle introduction to reflection
kim.mens
 
Managing the Evolution of Information Systems with Intensional Views and Rela...
kim.mens
 
Usage contracts (presented at SATToSE 2014 in L'Aquila, Italy)
kim.mens
 

Recently uploaded (20)

PDF
Unit-3 ppt.pdf organic chemistry - 3 unit 3
visionshukla007
 
PPTX
Phage Therapy and Bacteriophage Biology.pptx
Prachi Virat
 
PDF
Adding Geochemistry To Understand Recharge Areas - Kinney County, Texas - Jim...
Texas Alliance of Groundwater Districts
 
PPT
Restriction digestion of DNA for students of undergraduate and post graduate ...
DrMukeshRameshPimpli
 
PDF
The ALMA-CRISTAL survey: Gas, dust, and stars in star-forming galaxies when t...
Sérgio Sacani
 
PPTX
Diagnostic Features of Common Oral Ulcerative Lesions.pptx
Dr Palak borade
 
PPTX
LESSON 2 PSYCHOSOCIAL DEVELOPMENT.pptx L
JeanCarolColico1
 
PPTX
Bacillus thuringiensis.crops & golden rice
priyadharshini87125
 
PPTX
Akshay tunneling .pptx_20250331_165945_0000.pptx
akshaythaker18
 
PPTX
770043401-q1-Ppt-pe-and-Health-7-week-1-lesson-1.pptx
AizaRazonado
 
PPTX
Q1_Science 8_Week3-Day 1.pptx science lesson
AizaRazonado
 
PDF
High-speedBouldersandtheDebrisFieldinDARTEjecta
Sérgio Sacani
 
PPTX
GB1 Q1 04 Life in a Cell (1).pptx GRADE 11
JADE ACOSTA
 
PPTX
Systamatic Acquired Resistence (SAR).pptx
giriprasanthmuthuraj
 
PPTX
MODULE 2 Effects of Lifestyle in the Function of Respiratory and Circulator...
judithgracemangunday
 
PPTX
Envenomation AND ANIMAL BITES DETAILS.pptx
HARISH543351
 
PPTX
CNS.pptx Central nervous system meninges ventricles of brain it's structure a...
Ashwini I Chuncha
 
PDF
Carbon-richDustInjectedintotheInterstellarMediumbyGalacticWCBinaries Survives...
Sérgio Sacani
 
PDF
Preserving brand authenticity amid AI-driven misinformation: Sustaining consu...
Selcen Ozturkcan
 
PPTX
Q1 - W1 - D2 - Models of matter for science.pptx
RyanCudal3
 
Unit-3 ppt.pdf organic chemistry - 3 unit 3
visionshukla007
 
Phage Therapy and Bacteriophage Biology.pptx
Prachi Virat
 
Adding Geochemistry To Understand Recharge Areas - Kinney County, Texas - Jim...
Texas Alliance of Groundwater Districts
 
Restriction digestion of DNA for students of undergraduate and post graduate ...
DrMukeshRameshPimpli
 
The ALMA-CRISTAL survey: Gas, dust, and stars in star-forming galaxies when t...
Sérgio Sacani
 
Diagnostic Features of Common Oral Ulcerative Lesions.pptx
Dr Palak borade
 
LESSON 2 PSYCHOSOCIAL DEVELOPMENT.pptx L
JeanCarolColico1
 
Bacillus thuringiensis.crops & golden rice
priyadharshini87125
 
Akshay tunneling .pptx_20250331_165945_0000.pptx
akshaythaker18
 
770043401-q1-Ppt-pe-and-Health-7-week-1-lesson-1.pptx
AizaRazonado
 
Q1_Science 8_Week3-Day 1.pptx science lesson
AizaRazonado
 
High-speedBouldersandtheDebrisFieldinDARTEjecta
Sérgio Sacani
 
GB1 Q1 04 Life in a Cell (1).pptx GRADE 11
JADE ACOSTA
 
Systamatic Acquired Resistence (SAR).pptx
giriprasanthmuthuraj
 
MODULE 2 Effects of Lifestyle in the Function of Respiratory and Circulator...
judithgracemangunday
 
Envenomation AND ANIMAL BITES DETAILS.pptx
HARISH543351
 
CNS.pptx Central nervous system meninges ventricles of brain it's structure a...
Ashwini I Chuncha
 
Carbon-richDustInjectedintotheInterstellarMediumbyGalacticWCBinaries Survives...
Sérgio Sacani
 
Preserving brand authenticity amid AI-driven misinformation: Sustaining consu...
Selcen Ozturkcan
 
Q1 - W1 - D2 - Models of matter for science.pptx
RyanCudal3
 

Introduction to Smalltalk

  • 1. LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. Lecture 3 :
 Introduction to Smalltalk LSINF 2335 Programming Paradigms Prof. Kim Mens UCL / EPL / INGI (Slides revised with the help of
 Dr. Johan Brichau and partially based
 on slides by Kris Gybels and others) Disclaimer: any pictures used in this presentation remain with the copyright of their original owner and were included here for didactical purposes only. In case of any suspected violation of copyright please contact me so that I can remove or replace those pictures.
  • 2. LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. Smalltalk in a nutshell (1) ■ Pure object-oriented language : – Everything is an object – Objects communicate only by message passing ■ Class-based language : – Every object is an instance of a class – All classes have a (single) parent class – Object is the root class ■ Uniform & simple syntax – Forces one to think object-oriented – Syntax fits on a single sheet of paper – Statements read almost like English phrases – Originally designed as a learning environment for children ■ Dynamically typed ■ Fully reflective language – IDE entirely written (and modifiable) in Smalltalk itself 2
  • 3. LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. Smalltalk in a nutshell (2) ■ Platform independent – A runtime environment based on VM technology ■ Encourages exploratory programming ■ Memory management (garbage collection) ■ A large set of reusable classes – basic data structures, GUI classes, DB access, internet, graphics, … ■ Powerful development tools – browsers, GUI builders, inspectors, change management tools, crash recovery tools, project management tools, unit testing, refactoring, … ■ Team working environment – releasing, versioning, deploying ■ Source of inspiration to many other languages and environments – environment, collections, debugger, reflective features
 and many development tools 3
  • 4. LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. A byte-code interpreter: the virtual machine interpretes the image IMAGE2.IM IMAGE2.CHA Standard SOURCES Shared by everybody IMAGE1.IM IMAGE1.CHA All the objects of the system at a moment in time One per user + Smalltalk run-time architecture ■ Virtual Machine + Image + Changes and Sources ■ Image = bytecode ■ Sources and changes = code (text) 4
  • 5. LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. Point x, y Classes Concepts of Smalltalk: `pure’ OO 5 a Point x y a SmallInteger 5 a SmallInteger 10 Objects add: a SmallInteger 15Messages add: extra x := x + extra. y := y + extra. Methods
  • 6. LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. Everything is an object 6 a Person name address a ByteString ‘Kim Mens’ an Address street nr a ByteString ‘Pl. Sainte Barbe’ a SmallInteger 2 Objects have state
  • 7. LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. Everything is an object ■ Workspaces, windows, text editors, scrollbars, ... are all objects. ■ ‘hello word’ is an object: it is an instance of String. ■ #show: is a Symbol that is also an object. ■ Numbers are instances of classes: 1 is instance of Integer ■ Booleans are instances of their class: true is instance of class True ■ The parser, compiler and garbage collector are objects. ■ Even classes are objects themselves ■ 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. – Smalltalk is a fully reflective language
  • 8. LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. Objects communicate using messages 8 a SmallInteger 15 add: Objects can be sent messages ... add: extra x := x + extra. y := y + extra. ... and reply by executing a method ↺ a SmallInteger 20 + a SmallInteger 5 a SmallInteger 10 a Point x y a SmallInteger 25 +
  • 9. LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. Objects communicate using messages ■ A message is a request to an object to do something ■ A method is piece of source code belonging to a class ■ Transcript show: ‘hello world’ ■ The above expression is a message – Transcript is a global variable containing a Stream object that represents a “console” window. – show: ‘hello world’ is a message sent to a Stream object • The stream object (value of Transcript) is the receiver of the message • #show: is the selector of the message • the string ‘hello world’ is the (actual) argument of the message
  • 10. LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. Objects communicate using messages ■ An object executes a method when it receives a message. ■ ‘hello world’ reverse – Object ‘hello world’ receives a request to reverse itself – Method reverse is defined on the class String – Executing this method returns a new string ‘dlrow olleh’ ■ Transcript show: ‘hello world’ – Object Transcript receives a request to show a string and results in the string being printed on the transcript window. ■ Message passing or sending a message is similar to – invoking a method in Java or C++ – calling a procedure in procedural languages – applying a function in functional languages
  • 12. LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. Smalltalk basic expressions 12 Literals Variable Reference Message Expressions Return Expressions Block Expressions 1234 someObject someObject doSomethingWith: 1234 ^ someObject print: ‘Johan’ $a #Johan ‘Kim’ #(#a #b) a GlobalVariable [ :x :y | x + y ] 1 + 2 ‘Kim’ , ‘Mens’ #(#a #b) first
  • 13. LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. Literal Expressions 13 a ByteString ‘Pleinlaan’ a 42 a ByteSymbol #Brussels a Character $a ‘Pleinlaan’↺ = = = = 42 ↺ #Brussels ↺ $a ↺ #(1 #a $c) ↺ an Array 1 2 3 = a SmallInteger 1 a Character $c a ByteSymbol #a
  • 14. LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. Three kinds of messages 14 a SomeObjectsomeVar + add: with: a Number 42 a Number 100 printYourself
  • 15. LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. Unary message 15 printString an Assistant name a ByteString ‘Johan Brichau’ = a ByteString ‘Brichau J.’ selector is alphabetic name no arguments selector is printString
  • 16. LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. Unary message expressions 16 42 cos 42 cos sin ↺ = cos a SmallInteger 42 = a Float -0.399985 cos a SmallInteger 42 a Float -0.399985 ↺ sin = a Float -0.389405 =
  • 17. LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. Binary messages 17 a SmallInteger 10 + a SmallInteger 42 selector is one or two special characters exactly one argument (and one receiver)
  • 18. LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. Binary message expressions 18 1 + 2 cos 1 + (2 cos)= 1 + 2 cos - 3 (1 + (2 cos)) - 3= 1 + 2 cos + 3 (1 + (2 cos)) +3= 1 + 2 * 3 + 4 ((1 + 2) * 3) + 4= 1 + 2 * 3 + 4 1 + (2 * 3) + 4=!
  • 19. LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. a ByteString Boo’ Keyword messages 19 a SomeObjectsomeVar add: with: andPrint: a SmallInteger42 a SmallInteger100 Selector is sequence of names ending in colon (a keyword) One argument for each keyword selector is add:with:andPrint:
  • 20. LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. Keyword message expressions 20 Time readFrom: (ReadStream on: ‘1:30:00 pm’) Time readFrom: ReadStream on: ‘1:30:00 pm’ =
  • 21. LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. ■Unary message 
 >> binary message 
 >> keyword message ■ Evaluation from left to right ■ There are no specific precedence rules for the arithmetic or logic operators 2 negated raisedTo: 3 + 2 * 5 is equivalent with (2 negated) raisedTo: ((3 + 2) * 5) Precedence rules
  • 22. LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. ■ In Java different predefined syntactic constructs like >>, if, for, while, … are hard-coded into the language grammar ■ In Smalltalk, they are just defined on objects, e.g. : (>>) 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: [:each | Transcript show: each printString ] ■ Advantages : – Language is extensible (very easy to add “new syntax”) – Simplicity & orthogonality (uniform language design) Control structures
  • 23. LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. Blocks 23 [ :foo :bar |foo + bar ] ↺ a BlockClosure = value: value: Blocks are “delayed” expressions Blocks can be sent messages to evaluate the body of the block [ :foo :bar |foo + bar ] value: 4 value: 2 ↺ = a SmallInteger 6 a BlockClosure
  • 24. LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. Block expressions 24 [ Transcript show: 'Boo!'] value ablock := [ :someName | someName asUppercase ]. ablock value: 'Kim' [ :a :b| a + b ] valueWithArguments: (Array with: 1 with: 2)
  • 25. LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. Blocks example: if then else 25 a TrueifTrue: ifFalse: a BlockClosure a BlockClosure value
  • 26. LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. Blocks example: if then else 26 a FalseifTrue: ifFalse: a BlockClosure a BlockClosure value
  • 27. LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. Blocks 27 true ifTrue: [ Transcript show: ‘yes’ ] ifFalse: [ Transcript show: ‘no’ ] (a < b ) ifTrue: [ Transcript show: ‘a smaller’ ] ifFalse: [ Transcript show: ‘a not smaller’ ] Transcript show: ((a < b) ifTrue: [ ‘a’ ] ifFalse: [‘a not’ ]) , ‘ smaller’
  • 28. LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. ■ A method that illustrates every part of Smalltalk method syntax except primitives. uselessMethod: x |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 Power & Simplicity: Syntax on a PostCard
  • 29. LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. Power & Simplicity: Syntax on a PostCard ■ This (useless) method illustrates the entire Smalltalk syntax – messages • unary, binary, and key word messages • cascaded messages – variables • arguments and temporaries (local variables) ; no block temporaries • global variables • accessing and assigning a variable • pseudo variables (true, false, nil, self, super) – instructions • sequence of instructions (.) • return statement (^) – blocks (both zero argument and one argument block) – literals (array, character, symbol, string, integer, float)
  • 30. LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. “a comment” $c $t $e $r $# $@ ‘a string’ ‘lulu’ ‘l’’idiot’ #mac #+ #(1 2 3 (1 3) $a 4) 1, 2r101 1.5, 6.03e-34,4, 2.4e7 1/33 true, false 10@120 <primitive: …> ( … ) Smalltalk Syntax (1) ■ comment ■ character ■ string ■ symbol ■ array ■ integer (r = radix) ■ real (e = exponent) ■ fraction ■ boolean ■ point ■ VM primitive call ■ parenthesis (precedence)
  • 31. LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. var := aValue [:var | |tmp| expr...] |tmp| :var receiver selector receiver selector arg receiver kw1: arg1 kw2: arg2 message ; selector; … message . message ^ message Smalltalk Syntax (2) ■ assignment ■ a block ■ local variable (method / block) ■ block variable ■ unary message ■ binary message ■ keyword message ■ cascading of messages = sending several messages to the same instance ■ sequence of instructions ■ return statement
  • 32. LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. Smalltalk versus Java Smalltalk Java Syntax Simple Complex Control structures Defined by the programmer Predefined Typing Dynamic Static (primitive types, type casts) Reflection Full reflection Introspection only (see but not modify) Garbage collection Yes Yes Virtual machine Yes Yes Concurrency Processes Threads
  • 33. LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. Smalltalk versus Java Smalltalk Java Visibility of variables Protected Public, protected, private Visibility of methods Public Public, protected, private Dynamicity of methods Dynamic Dynamic or static Interfaces No Yes
  • 34. LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. Smalltalk versus Java ■ Java and C++ have a (too) complex syntax – The constructs for expressing conditionnals, iteration, arithmetic and so on are predefined in the syntax – Conditionnals • if (BooleanExpression) { /* do something */ } • if (BooleanExpression) { /* do something */ }
 else { /* something else */ } • case ... – Iteration • for (i=1; i<length; i++) { /* do something */ } • while (BooleanExpression) { /* do something */ } – Arithmetic • 2 + 4 • 3 * 7 • 10 << 2 (bitshift)
  • 35. LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. Smalltalk versus Java ■ Smalltalk has a much more simple and uniform syntax – Everything is a message, even • Conditionnals • Boolean operators • Arithmetic operators • Iteration – The language is extensible • Define your own language constructs
  • 36. LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. Homework and exercises 36 ■ Download and install Pharo on your computer – Pharo can be downloaded on http://pharo-project.org – Download the latest stable version ■ Download and read the free online book on Pharo – “Pharo by Example” can be downloaded on
 http://pharobyexample.org – Read and try chapters 1 to 6 of the book – (attention: the book may not have been updated yet to the most recent version of Pharo) ■ Also try out the examples from this theory session.