SlideShare une entreprise Scribd logo
1  sur  54
2. Smalltalk Basics
Birds-eye view
© Oscar Nierstrasz
ST — Introduction
1.2
Less is More — simple syntax and semantics
uniformly applied can lead to an expressive and
flexible system, not an impoverished one.
Less is More — simple syntax and semantics
uniformly applied can lead to an expressive and
flexible system, not an impoverished one.
© Oscar Nierstrasz
ST — Smalltalk Basics
2.3
Roadmap
> Everything is an Object
> Syntax on a Postcard
> Three Kinds of Messages
> Methods, variables and blocks
> Test-Driven Development
> Managing Projects
Selected material courtesy Stéphane Ducasse
© Oscar Nierstrasz
ST — Smalltalk Basics
2.4
Roadmap
> Everything is an Object
> Syntax on a Postcard
> Three Kinds of Messages
> Methods, variables and blocks
> Test-Driven Development
> Managing Projects
© Oscar Nierstrasz
ST — Smalltalk Basics
2.5
Objects in Smalltalk
> Everything is an object
— Things only happen by message passing
— Variables are dynamically bound
> State is private to objects
— “protected” for subclasses
> Methods are public
— “private” methods by convention only
> (Nearly) every object is a reference
— Unused objects are garbage-collected
> Single inheritance
© Oscar Nierstrasz
ST — Smalltalk Basics
2.6
Accept, DoIt, PrintIt and InspectIt
> Accept
— Compile a method or a class definition
> DoIt
— Evaluate an expression
> PrintIt
— Evaluate an expression and print the
result (#printOn:)
> InspectIt
— Evaluate an expression and inspect the
result (#inspect)
© Oscar Nierstrasz
ST — Smalltalk Basics
2.7
Hello World
> At anytime, in any tool, 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 show: 'hello world'
© Oscar Nierstrasz
ST — Smalltalk Basics
2.8
“Hello World”
> Transcript is a kind of “standard output”
— a TextCollector instance associated with the launcher.
© Oscar Nierstrasz
ST — Smalltalk Basics
2.9
Everything is an Object
> 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.
– The workspace is an object.
– The window is an instance of SystemWindow.
– The text editor is an instance of ParagraphEditor.
– The scrollbars are objects too.
– 'hello word' is an instance of String.
– #show: is a Symbol
– The mouse is an object.
– The parser is an instance of Parser.
– The compiler is an instance of Compiler.
– The process scheduler is also an object.
© Oscar Nierstrasz
ST — Smalltalk Basics
2.10
Roadmap
> Everything is an Object
> Syntax on a Postcard
> Three Kinds of Messages
> Methods, variables and blocks
> Test-Driven Development
> Managing Projects
© Oscar Nierstrasz
ST — Smalltalk Basics
2.11
Smalltalk 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
© Oscar Nierstrasz
ST — Smalltalk Basics
2.12
Language Constructs
^ return
"…" comment
# symbol or array
'…' string
[ ] block or byte array (VisualWorks)
. statement separator
; message cascade
|…| local or block variable
:= assignment (also _ or ←)
$_ character
: end of selector name
_e_ _r_ number exponent or radix
! file element separator (used in change sets)
<primitive: ...> for VM primitive calls
© Oscar Nierstrasz
ST — Smalltalk Basics
2.13
Examples
comment: "a comment"
character: $c $h $a $r $a $c $t $e $r $s $# $@
string: 'a nice string'
symbol: #mac #+
array: #(1 2 3 (1 3) $a 4)
dynamic array (Pharo): { 1 + 2 . 3 / 4 }
Integer: 1, 2r101
real: 1.5, 6.03e-34,4, 2.4e7
fraction: 1/33
boolean: true, false
pseudo variables self, super
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: ...
© Oscar Nierstrasz
ST — Smalltalk Basics
2.14
Roadmap
> Everything is an Object
> Syntax on a Postcard
> Three Kinds of Messages
> Methods, variables and blocks
> Test-Driven Development
> Managing Projects
© Oscar Nierstrasz
ST — Smalltalk Basics
2.15
Messages instead of keywords
> In most languages, basic operators and control constructs are
defined as language constructs and keywords
> In Smalltalk, there are only messages sent to objects
— bitShift: (>>) is just a message sent to a number
— ifTrue: (if-then-else) is just a message sent to a boolean
— do:, to:do: (loops) are just messages to collections or numbers
> Minimal parsing
> Language is extensible
10 bitShift: 2
(x>1) ifTrue: [ Transcript show: 'bigger' ]
#(a b c d) do: [:each | Transcript show: each ; cr]
1 to: 10 do: [:i | Transcript show: i printString; cr]
© Oscar Nierstrasz
ST — Smalltalk Basics
2.16
Smalltalk Syntax
Every expression is a message send
> Unary messages
> Binary messages
> Keyword messages
Transcript cr
5 factorial
3 + 4
Transcript show: 'hello world'
2 raisedTo: 32
3 raisedTo: 10 modulo: 5
© Oscar Nierstrasz
ST — Smalltalk Basics
2.17
Precedence
(…) > Unary > Binary > Keyword
1. Evaluate left-to-right
2. Unary messages have highest precedence
3. Next are binary messages
4. Keyword messages have lowest precedence
5. Use parentheses to change precedence
2 raisedTo: 1 + 3 factorial
1 + 2 * 3
1 + (2 * 3)
128
9 (!)
7
© Oscar Nierstrasz
ST — Smalltalk Basics
2.18
Binary Messages
> Syntax:
— aReceiver aSelector anArgument
— Where aSelector is made up of 1 or 2 characters from:
+ - /  * ~ < > = @ % | & ! ? ,
— Except: second character may not be $
> Examples: 2 * 3 - 5
5 >= 7
6 = 7
'hello', ' ', 'world'
(3@4) + (1@2)
2<<5
64>>5
© Oscar Nierstrasz
ST — Smalltalk Basics
2.19
Roadmap
> Everything is an Object
> Syntax on a Postcard
> Three Kinds of Messages
> Methods, variables and blocks
> Test-Driven Development
> Managing Projects
© Oscar Nierstrasz
ST — Smalltalk Basics
2.20
More syntax
> Comments are enclosed in double quotes
> Use periods to separate expressions
> Use semi-colons to send a cascade of messages to the
same object
"This is a comment."
Transcript cr; show: 'hello world'; cr
Transcript cr.
Transcript show: 'hello world’.
Transcript cr "NB: don’t need one here"
© Oscar Nierstrasz
ST — Smalltalk Basics
2.21
Variables
> Declare local variables with | … |
> Use := to assign a value to a variable
> Old fashioned assignment operator: ← (must type “_”)
| x y |
x := 1
© Oscar Nierstrasz
ST — Smalltalk Basics
2.22
Method Return
> Use a caret to return a value from a method or a block
> Methods always return a value
— By default, methods return self
max: aNumber
^ self < aNumber
ifTrue: [aNumber]
ifFalse: [self] 1 max: 2 2
© Oscar Nierstrasz
ST — Smalltalk Basics
2.23
Block closures
> Use square brackets to delay evaluation of expressions
^ 1 < 2 ifTrue: ['smaller'] ifFalse: ['bigger']
'smaller'
© Oscar Nierstrasz
ST — Smalltalk Basics
2.24
Variables
> Local variables within methods (or blocks) are delimited
by |var|
> Block parameters are delimited by :var|
OrderedCollection>>collect: aBlock
"Evaluate aBlock with each of my elements as the argument."
| newCollection |
newCollection := self species new: self size.
firstIndex to: lastIndex do:
[ :index |
newCollection addLast: (aBlock value: (array at: index))].
^ newCollection
[:n | |x y| x := n+1. y := n-1. x * y] value: 10 99
© Oscar Nierstrasz
ST — Smalltalk Basics
2.25
Control Structures
> Every control structure is realized by message sends
|n|
n := 10.
[n>0] whileTrue:
[ Transcript show: n; cr.
n := n-1 ]
1 to: 10 do: [:n| Transcript show: n; cr ]
(1 to: 10) do: [:n| Transcript show: n; cr ]
© Oscar Nierstrasz
ST — Smalltalk Basics
2.26
Creating objects
> Class methods
> Factory methods
OrderedCollection new
Array with: 1 with: 2
1@2 "a Point"
1/2 "a Fraction"
© Oscar Nierstrasz
ST — Smalltalk Basics
2.27
Creating classes
> Send a message to a class (!)
Number subclass: #Complex
instanceVariableNames: 'real imaginary'
classVariableNames: ''
poolDictionaries: ''
category: 'ComplexNumbers'
© Oscar Nierstrasz
ST — Smalltalk Basics
2.28
Some Conventions
> Method selector is a symbol, e.g., #add:
> Method scope conventions using >>
— Instance Method defined in the class Node
— Class Method defined in the class Node class
(i.e., in the class of the the class Node)
> aSomething is an instance of the class Something
Node>>accept: aPacket
Node class>>withName: aSymbol
© Oscar Nierstrasz
ST — Smalltalk Basics
2.29
Roadmap
> Everything is an Object
> Syntax on a Postcard
> Three Kinds of Messages
> Methods, variables and blocks
> Test-Driven Development
> Managing Projects
© Oscar Nierstrasz
ST — Smalltalk Basics
2.30
Change sets
> Make sure your changes are logged to a new change set
© Oscar Nierstrasz
ST — Smalltalk Basics
2.31
SUnit
© Oscar Nierstrasz
ST — Smalltalk Basics
2.32
Money
TestCase subclass: #MoneyTest
instanceVariableNames: 'chf2 chf8 chf10'
classVariableNames: ''
poolDictionaries: ''
category: 'Money'
> We will implement the Money example in Smalltalk
— First, we develop a test case for a single currency
NB: This is just a message sent to the TestCase class object (!)
© Oscar Nierstrasz
ST — Smalltalk Basics
2.33
SetUp
> We will need setters for the private Money state
MoneyTest>>setUp
chf2 := Money new currency: 'CHF'; amount: 2.
chf8 := Money new currency: 'CHF'; amount: 8.
chf10 := Money new currency: 'CHF'; amount: 10.
© Oscar Nierstrasz
LECTURE TITLE
34
Protocols
Classify methods into protocols that reveal their intent
© Oscar Nierstrasz
ST — Smalltalk Basics
2.35
MoneyTest>>testEquals
> Some obvious tests
MoneyTest>>testEquals
self assert: chf2 = chf2.
self assert: chf2 = (Money new currency: 'CHF'; amount: 2).
self assert: chf2 != chf8.
© Oscar Nierstrasz
ST — Smalltalk Basics
2.36
Money
> We define Money as a subclass of Object, with getters
and setters
Object subclass: #Money
instanceVariableNames: 'currency amount'
classVariableNames: ''
poolDictionaries: ''
category: 'Money'
Money>>currency: aString
currency := aString.
Money>>currency
^ currency
Money>>amount: aNumber
amount:= aNumber.
Money>>amount
^ amount
© Oscar Nierstrasz
ST — Smalltalk Basics
2.37
Failing tests
© Oscar Nierstrasz
ST — Smalltalk Basics
2.38
Comparisons
Money>>= aMoney
^ self currency = aMoney currency
and: [ self amount = aMoney amount ]
Money>>!= aMoney
^ (self = aMoney) not
© Oscar Nierstrasz
ST — Smalltalk Basics
2.39
Constructors
MoneyTest>>testEquals
self assert: chf2 = chf2.
self assert: chf2 =
(Money currency: 'CHF' amount: 2).
self assert: chf2 != chf8.
We need a constructor on the class side of Money
© Oscar Nierstrasz
ST — Smalltalk Basics
2.40
Class methods
NB: Which “self” is referred to in the method body?
© Oscar Nierstrasz
ST — Smalltalk Basics
2.41
Addition
Money>>+ aMoney
^ Money currency: self currency
amount: self amount + aMoney amount
MoneyTest>>testAdd
self assert: chf2 + chf8 = chf10
And so on …
© Oscar Nierstrasz
ST — Smalltalk Basics
2.42
Filing out your changes
> You can “file out” all your changes so they can be
loaded into another image
© Oscar Nierstrasz
ST — Smalltalk Basics
2.43
Change Sets
'From Pharo1.0beta of 16 May 2008 [Latest update: #10418] on 8 September 2009
at 1:58:51 pm'!
Object subclass: #Money
instanceVariableNames: 'currency amount'
classVariableNames: ''
poolDictionaries: ''
category: 'Money'!
TestCase subclass: #TestMoney
instanceVariableNames: 'chf2 chf8 chf10'
classVariableNames: ''
poolDictionaries: ''
category: 'Money'!
!Money methodsFor: 'as yet unclassified' stamp: 'on 7/2/2007 13:21'!
!!= aMoney
^ (self = aMoney) not! !
!Money methodsFor: 'as yet unclassified' stamp: 'on 7/2/2007 13:26'!
+ aMoney
self assert: [ self currency = aMoney currency ].
^ Money currency: self currency amount: self amount + aMoney amount! !
!Money methodsFor: 'as yet unclassified' stamp: 'on 7/2/2007 13:20'!
= aMoney
^ self amount = aMoney amount and: [ self currency = aMoney currency ]! !
...
© Oscar Nierstrasz
ST — Smalltalk Basics
2.44
Extensions
> Creating new instances of Money is still cumbersome
— We would like to be able to write instead code like this:
TestMoney>>setUp
chf2 := 2 chf.
chf8 := 8 chf.
chf10 := 10 chf.
Extension protocols
© Oscar Nierstrasz
ST — Smalltalk Basics
2.45
To extend an existing class for a category MyProject, define
the protocol *MyProject (or *myproject ) for that class.
© Oscar Nierstrasz
ST — Smalltalk Basics
2.46
Roadmap
> Everything is an Object
> Syntax on a Postcard
> Three Kinds of Messages
> Methods, variables and blocks
> Test-Driven Development
> Managing Projects
© Oscar Nierstrasz
ST — Smalltalk Basics
2.47
SqueakSource.com
© Oscar Nierstrasz
ST — Smalltalk Basics
2.48
Categories, Projects and Packages
> A system category MyProject (and possibly
MyProject-*) contains the classes of your application
> A Monticello package MyProject contains the
categories MyProject and MyProject-*
— NB: If you have sub-categories, keep the top one empty!
> A SqueakSource project MyProject stores everything
in the Monticello package MyProject
© Oscar Nierstrasz
ST — Smalltalk Basics
2.49
Loading a project
> To load a project from SqueakSource.com
— Find the project in SqueakSource
— Open the Monticello Browser
— Add an HTTP Repository
– Accept the code fragment from the SqueakSource page
— Open the repository and load the latest version
© Oscar Nierstrasz
ST — Smalltalk Basics
2.50
Loading a project
© Oscar Nierstrasz
ST — Smalltalk Basics
2.51
Creating a project
> To create a project on SqueakSource.com
— Define your categories MyProject or MyProject-*
— Create a SqueakSource project named MyProject
— Define a Monticello package MyProject
— Add an HTTP Repository for MyProject
– Accept the code fragment from the SqueakSource page
— Save the package
© Oscar Nierstrasz
ST — Smalltalk Basics
2.52
What you should know!
How can you indicate that a method is “private”?
What is the difference between a comment and a
string?
Why does 1+2*3 = 9?
What is a cascade?
How is a block like a lambda expression?
How do you create a new class?
How do you inspect an object?
© Oscar Nierstrasz
ST — Smalltalk Basics
2.53
Can you answer these questions?
Why does Smalltalk support single (and not multiple)
inheritance?
Is the cascade strictly necessary?
Why do you need to declare local variables if there are
no static types?
How can you discover the class of a GUI object?
How does SUnit differ from JUnit?
© Oscar Nierstrasz
ST — Introduction
1.54
Attribution-ShareAlike 3.0 Unported
You are free:
to Share — to copy, distribute and transmit the work
to Remix — to adapt the work
Under the following conditions:
Attribution. You must attribute the work in the manner specified by the author or
licensor (but not in any way that suggests that they endorse you or your use of the
work).
Share Alike. If you alter, transform, or build upon this work, you may distribute the
resulting work only under the same, similar or a compatible license.
For any reuse or distribution, you must make clear to others the license terms of this work.
The best way to do this is with a link to this web page.
Any of the above conditions can be waived if you get permission from the copyright holder.
Nothing in this license impairs or restricts the author's moral rights.
License
http://creativecommons.org/licenses/by-sa/3.0/

Contenu connexe

Tendances

4 - OOP - Taste of Smalltalk (Tamagoshi)
4 - OOP - Taste of Smalltalk (Tamagoshi)4 - OOP - Taste of Smalltalk (Tamagoshi)
4 - OOP - Taste of Smalltalk (Tamagoshi)The World of Smalltalk
 
Lotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScript
Lotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScriptLotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScript
Lotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScriptBill Buchan
 
Class notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritanceClass notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritanceKuntal Bhowmick
 
Dotnet framework difference faqs- 2
Dotnet framework difference faqs- 2Dotnet framework difference faqs- 2
Dotnet framework difference faqs- 2Umar Ali
 

Tendances (20)

11 bytecode
11 bytecode11 bytecode
11 bytecode
 
09 metaclasses
09 metaclasses09 metaclasses
09 metaclasses
 
8 - OOP - Syntax & Messages
8 - OOP - Syntax & Messages8 - OOP - Syntax & Messages
8 - OOP - Syntax & Messages
 
13 traits
13 traits13 traits
13 traits
 
8 - OOP - Smalltalk Syntax
8 - OOP - Smalltalk Syntax8 - OOP - Smalltalk Syntax
8 - OOP - Smalltalk Syntax
 
4 - OOP - Taste of Smalltalk (Squeak)
4 - OOP - Taste of Smalltalk (Squeak)4 - OOP - Taste of Smalltalk (Squeak)
4 - OOP - Taste of Smalltalk (Squeak)
 
Stoop ed-class forreuse
Stoop ed-class forreuseStoop ed-class forreuse
Stoop ed-class forreuse
 
Stoop sed-sharing ornot
Stoop sed-sharing ornotStoop sed-sharing ornot
Stoop sed-sharing ornot
 
4 - OOP - Taste of Smalltalk (Tamagoshi)
4 - OOP - Taste of Smalltalk (Tamagoshi)4 - OOP - Taste of Smalltalk (Tamagoshi)
4 - OOP - Taste of Smalltalk (Tamagoshi)
 
Lotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScript
Lotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScriptLotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScript
Lotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScript
 
Class notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritanceClass notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritance
 
9 - OOP - Smalltalk Classes (a)
9 - OOP - Smalltalk Classes (a)9 - OOP - Smalltalk Classes (a)
9 - OOP - Smalltalk Classes (a)
 
Stoop ed-some principles
Stoop ed-some principlesStoop ed-some principles
Stoop ed-some principles
 
Stoop 423-smalltalk idioms
Stoop 423-smalltalk idiomsStoop 423-smalltalk idioms
Stoop 423-smalltalk idioms
 
Dotnet framework difference faqs- 2
Dotnet framework difference faqs- 2Dotnet framework difference faqs- 2
Dotnet framework difference faqs- 2
 
Inheritance
InheritanceInheritance
Inheritance
 
Jscript part2
Jscript part2Jscript part2
Jscript part2
 
Stoop 414-smalltalk elementsofdesign
Stoop 414-smalltalk elementsofdesignStoop 414-smalltalk elementsofdesign
Stoop 414-smalltalk elementsofdesign
 
9 - OOP - Smalltalk Classes (b)
9 - OOP - Smalltalk Classes (b)9 - OOP - Smalltalk Classes (b)
9 - OOP - Smalltalk Classes (b)
 
iOS Session-2
iOS Session-2iOS Session-2
iOS Session-2
 

En vedette (20)

1 - OOP
1 - OOP1 - OOP
1 - OOP
 
2 - OOP
2 - OOP2 - OOP
2 - OOP
 
Stoop ed-frameworks
Stoop ed-frameworksStoop ed-frameworks
Stoop ed-frameworks
 
Stoop metaclasses
Stoop metaclassesStoop metaclasses
Stoop metaclasses
 
Stoop 423-some designpatterns
Stoop 423-some designpatternsStoop 423-some designpatterns
Stoop 423-some designpatterns
 
Stoop 433-chain
Stoop 433-chainStoop 433-chain
Stoop 433-chain
 
Stoop 416-lsp
Stoop 416-lspStoop 416-lsp
Stoop 416-lsp
 
11 - OOP - Numbers
11 - OOP - Numbers11 - OOP - Numbers
11 - OOP - Numbers
 
Stoop 431-visitor
Stoop 431-visitorStoop 431-visitor
Stoop 431-visitor
 
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)
 
Stoop 430-design patternsintro
Stoop 430-design patternsintroStoop 430-design patternsintro
Stoop 430-design patternsintro
 
8 - OOP - Smalltalk Model
8 - OOP - Smalltalk Model8 - OOP - Smalltalk Model
8 - OOP - Smalltalk Model
 
Stoop 400 o-metaclassonly
Stoop 400 o-metaclassonlyStoop 400 o-metaclassonly
Stoop 400 o-metaclassonly
 
Stoop 415-design points
Stoop 415-design pointsStoop 415-design points
Stoop 415-design points
 
7 - OOP - OO Concepts
7 - OOP - OO Concepts7 - OOP - OO Concepts
7 - OOP - OO Concepts
 
10 - OOP - Inheritance (b)
10 - OOP - Inheritance (b)10 - OOP - Inheritance (b)
10 - OOP - Inheritance (b)
 
Double Dispatch
Double DispatchDouble Dispatch
Double Dispatch
 
Stoop sed-class initialization
Stoop sed-class initializationStoop sed-class initialization
Stoop sed-class initialization
 
05 seaside
05 seaside05 seaside
05 seaside
 
Stoop 434-composite
Stoop 434-compositeStoop 434-composite
Stoop 434-composite
 

Similaire à Smalltalk Basics Syntax and Objects

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
 
Pharo: Syntax in a Nutshell
Pharo: Syntax in a NutshellPharo: Syntax in a Nutshell
Pharo: Syntax in a NutshellMarcus Denker
 
5 Steps to Mastering the Art of Seaside
5 Steps to Mastering the Art of Seaside5 Steps to Mastering the Art of Seaside
5 Steps to Mastering the Art of SeasideLukas Renggli
 
Seattle Scalability Meetup 6-26-13
Seattle Scalability Meetup 6-26-13Seattle Scalability Meetup 6-26-13
Seattle Scalability Meetup 6-26-13specialk29
 
Working with NS2
Working with NS2Working with NS2
Working with NS2chanchal214
 
The Art Of Readable Code
The Art Of Readable CodeThe Art Of Readable Code
The Art Of Readable CodeBaidu, Inc.
 
NET Systems Programming Learned the Hard Way.pptx
NET Systems Programming Learned the Hard Way.pptxNET Systems Programming Learned the Hard Way.pptx
NET Systems Programming Learned the Hard Way.pptxpetabridge
 
Pharo, an innovative and open-source Smalltalk
Pharo, an innovative and open-source SmalltalkPharo, an innovative and open-source Smalltalk
Pharo, an innovative and open-source SmalltalkSerge Stinckwich
 
Introduction to-linux
Introduction to-linuxIntroduction to-linux
Introduction to-linuxkishore1986
 
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 2009Pharo
 
2013 lecture-02-syntax shortnewcut
2013 lecture-02-syntax shortnewcut2013 lecture-02-syntax shortnewcut
2013 lecture-02-syntax shortnewcutPharo
 
ElasticSearch for .NET Developers
ElasticSearch for .NET DevelopersElasticSearch for .NET Developers
ElasticSearch for .NET DevelopersBen van Mol
 
Workshop on command line tools - day 2
Workshop on command line tools - day 2Workshop on command line tools - day 2
Workshop on command line tools - day 2Leandro Lima
 
Oh , I broke the build!
Oh , I broke the build!Oh , I broke the build!
Oh , I broke the build!Rainer Sigwald
 

Similaire à Smalltalk Basics Syntax and Objects (20)

4 - OOP - Taste of Smalltalk (VisualWorks)
4 - OOP - Taste of Smalltalk (VisualWorks)4 - OOP - Taste of Smalltalk (VisualWorks)
4 - OOP - Taste of Smalltalk (VisualWorks)
 
Pharo: Syntax in a Nutshell
Pharo: Syntax in a NutshellPharo: Syntax in a Nutshell
Pharo: Syntax in a Nutshell
 
Stoop 305-reflective programming5
Stoop 305-reflective programming5Stoop 305-reflective programming5
Stoop 305-reflective programming5
 
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)
 
Bioinformatics v2014 wim_vancriekinge
Bioinformatics v2014 wim_vancriekingeBioinformatics v2014 wim_vancriekinge
Bioinformatics v2014 wim_vancriekinge
 
5 Steps to Mastering the Art of Seaside
5 Steps to Mastering the Art of Seaside5 Steps to Mastering the Art of Seaside
5 Steps to Mastering the Art of Seaside
 
06 debugging
06 debugging06 debugging
06 debugging
 
Seattle Scalability Meetup 6-26-13
Seattle Scalability Meetup 6-26-13Seattle Scalability Meetup 6-26-13
Seattle Scalability Meetup 6-26-13
 
Working with NS2
Working with NS2Working with NS2
Working with NS2
 
The Art Of Readable Code
The Art Of Readable CodeThe Art Of Readable Code
The Art Of Readable Code
 
NET Systems Programming Learned the Hard Way.pptx
NET Systems Programming Learned the Hard Way.pptxNET Systems Programming Learned the Hard Way.pptx
NET Systems Programming Learned the Hard Way.pptx
 
01 intro
01 intro01 intro
01 intro
 
Pharo, an innovative and open-source Smalltalk
Pharo, an innovative and open-source SmalltalkPharo, an innovative and open-source Smalltalk
Pharo, an innovative and open-source Smalltalk
 
Introduction to-linux
Introduction to-linuxIntroduction to-linux
Introduction to-linux
 
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
 
Refactoring
RefactoringRefactoring
Refactoring
 
2013 lecture-02-syntax shortnewcut
2013 lecture-02-syntax shortnewcut2013 lecture-02-syntax shortnewcut
2013 lecture-02-syntax shortnewcut
 
ElasticSearch for .NET Developers
ElasticSearch for .NET DevelopersElasticSearch for .NET Developers
ElasticSearch for .NET Developers
 
Workshop on command line tools - day 2
Workshop on command line tools - day 2Workshop on command line tools - day 2
Workshop on command line tools - day 2
 
Oh , I broke the build!
Oh , I broke the build!Oh , I broke the build!
Oh , I broke the build!
 

Plus de The World of Smalltalk (11)

05 seaside canvas
05 seaside canvas05 seaside canvas
05 seaside canvas
 
Stoop sed-smells
Stoop sed-smellsStoop sed-smells
Stoop sed-smells
 
Stoop sed-class initialization
Stoop sed-class initializationStoop sed-class initialization
Stoop sed-class initialization
 
Stoop ed-unit ofreuse
Stoop ed-unit ofreuseStoop ed-unit ofreuse
Stoop ed-unit ofreuse
 
Stoop ed-subtyping subclassing
Stoop ed-subtyping subclassingStoop ed-subtyping subclassing
Stoop ed-subtyping subclassing
 
Stoop ed-lod
Stoop ed-lodStoop ed-lod
Stoop ed-lod
 
Stoop ed-inheritance composition
Stoop ed-inheritance compositionStoop ed-inheritance composition
Stoop ed-inheritance composition
 
Stoop ed-dual interface
Stoop ed-dual interfaceStoop ed-dual interface
Stoop ed-dual interface
 
Stoop 450-s unit
Stoop 450-s unitStoop 450-s unit
Stoop 450-s unit
 
Stoop 440-adaptor
Stoop 440-adaptorStoop 440-adaptor
Stoop 440-adaptor
 
Stoop 439-decorator
Stoop 439-decoratorStoop 439-decorator
Stoop 439-decorator
 

Dernier

Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
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 ModeThiyagu K
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
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 17Celine George
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
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 ImpactPECB
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 

Dernier (20)

Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
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
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
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
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
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
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 

Smalltalk Basics Syntax and Objects

  • 2. Birds-eye view © Oscar Nierstrasz ST — Introduction 1.2 Less is More — simple syntax and semantics uniformly applied can lead to an expressive and flexible system, not an impoverished one. Less is More — simple syntax and semantics uniformly applied can lead to an expressive and flexible system, not an impoverished one.
  • 3. © Oscar Nierstrasz ST — Smalltalk Basics 2.3 Roadmap > Everything is an Object > Syntax on a Postcard > Three Kinds of Messages > Methods, variables and blocks > Test-Driven Development > Managing Projects Selected material courtesy Stéphane Ducasse
  • 4. © Oscar Nierstrasz ST — Smalltalk Basics 2.4 Roadmap > Everything is an Object > Syntax on a Postcard > Three Kinds of Messages > Methods, variables and blocks > Test-Driven Development > Managing Projects
  • 5. © Oscar Nierstrasz ST — Smalltalk Basics 2.5 Objects in Smalltalk > Everything is an object — Things only happen by message passing — Variables are dynamically bound > State is private to objects — “protected” for subclasses > Methods are public — “private” methods by convention only > (Nearly) every object is a reference — Unused objects are garbage-collected > Single inheritance
  • 6. © Oscar Nierstrasz ST — Smalltalk Basics 2.6 Accept, DoIt, PrintIt and InspectIt > Accept — Compile a method or a class definition > DoIt — Evaluate an expression > PrintIt — Evaluate an expression and print the result (#printOn:) > InspectIt — Evaluate an expression and inspect the result (#inspect)
  • 7. © Oscar Nierstrasz ST — Smalltalk Basics 2.7 Hello World > At anytime, in any tool, 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 show: 'hello world'
  • 8. © Oscar Nierstrasz ST — Smalltalk Basics 2.8 “Hello World” > Transcript is a kind of “standard output” — a TextCollector instance associated with the launcher.
  • 9. © Oscar Nierstrasz ST — Smalltalk Basics 2.9 Everything is an Object > 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. – The workspace is an object. – The window is an instance of SystemWindow. – The text editor is an instance of ParagraphEditor. – The scrollbars are objects too. – 'hello word' is an instance of String. – #show: is a Symbol – The mouse is an object. – The parser is an instance of Parser. – The compiler is an instance of Compiler. – The process scheduler is also an object.
  • 10. © Oscar Nierstrasz ST — Smalltalk Basics 2.10 Roadmap > Everything is an Object > Syntax on a Postcard > Three Kinds of Messages > Methods, variables and blocks > Test-Driven Development > Managing Projects
  • 11. © Oscar Nierstrasz ST — Smalltalk Basics 2.11 Smalltalk 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
  • 12. © Oscar Nierstrasz ST — Smalltalk Basics 2.12 Language Constructs ^ return "…" comment # symbol or array '…' string [ ] block or byte array (VisualWorks) . statement separator ; message cascade |…| local or block variable := assignment (also _ or ←) $_ character : end of selector name _e_ _r_ number exponent or radix ! file element separator (used in change sets) <primitive: ...> for VM primitive calls
  • 13. © Oscar Nierstrasz ST — Smalltalk Basics 2.13 Examples comment: "a comment" character: $c $h $a $r $a $c $t $e $r $s $# $@ string: 'a nice string' symbol: #mac #+ array: #(1 2 3 (1 3) $a 4) dynamic array (Pharo): { 1 + 2 . 3 / 4 } Integer: 1, 2r101 real: 1.5, 6.03e-34,4, 2.4e7 fraction: 1/33 boolean: true, false pseudo variables self, super 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: ...
  • 14. © Oscar Nierstrasz ST — Smalltalk Basics 2.14 Roadmap > Everything is an Object > Syntax on a Postcard > Three Kinds of Messages > Methods, variables and blocks > Test-Driven Development > Managing Projects
  • 15. © Oscar Nierstrasz ST — Smalltalk Basics 2.15 Messages instead of keywords > In most languages, basic operators and control constructs are defined as language constructs and keywords > In Smalltalk, there are only messages sent to objects — bitShift: (>>) is just a message sent to a number — ifTrue: (if-then-else) is just a message sent to a boolean — do:, to:do: (loops) are just messages to collections or numbers > Minimal parsing > Language is extensible 10 bitShift: 2 (x>1) ifTrue: [ Transcript show: 'bigger' ] #(a b c d) do: [:each | Transcript show: each ; cr] 1 to: 10 do: [:i | Transcript show: i printString; cr]
  • 16. © Oscar Nierstrasz ST — Smalltalk Basics 2.16 Smalltalk Syntax Every expression is a message send > Unary messages > Binary messages > Keyword messages Transcript cr 5 factorial 3 + 4 Transcript show: 'hello world' 2 raisedTo: 32 3 raisedTo: 10 modulo: 5
  • 17. © Oscar Nierstrasz ST — Smalltalk Basics 2.17 Precedence (…) > Unary > Binary > Keyword 1. Evaluate left-to-right 2. Unary messages have highest precedence 3. Next are binary messages 4. Keyword messages have lowest precedence 5. Use parentheses to change precedence 2 raisedTo: 1 + 3 factorial 1 + 2 * 3 1 + (2 * 3) 128 9 (!) 7
  • 18. © Oscar Nierstrasz ST — Smalltalk Basics 2.18 Binary Messages > Syntax: — aReceiver aSelector anArgument — Where aSelector is made up of 1 or 2 characters from: + - / * ~ < > = @ % | & ! ? , — Except: second character may not be $ > Examples: 2 * 3 - 5 5 >= 7 6 = 7 'hello', ' ', 'world' (3@4) + (1@2) 2<<5 64>>5
  • 19. © Oscar Nierstrasz ST — Smalltalk Basics 2.19 Roadmap > Everything is an Object > Syntax on a Postcard > Three Kinds of Messages > Methods, variables and blocks > Test-Driven Development > Managing Projects
  • 20. © Oscar Nierstrasz ST — Smalltalk Basics 2.20 More syntax > Comments are enclosed in double quotes > Use periods to separate expressions > Use semi-colons to send a cascade of messages to the same object "This is a comment." Transcript cr; show: 'hello world'; cr Transcript cr. Transcript show: 'hello world’. Transcript cr "NB: don’t need one here"
  • 21. © Oscar Nierstrasz ST — Smalltalk Basics 2.21 Variables > Declare local variables with | … | > Use := to assign a value to a variable > Old fashioned assignment operator: ← (must type “_”) | x y | x := 1
  • 22. © Oscar Nierstrasz ST — Smalltalk Basics 2.22 Method Return > Use a caret to return a value from a method or a block > Methods always return a value — By default, methods return self max: aNumber ^ self < aNumber ifTrue: [aNumber] ifFalse: [self] 1 max: 2 2
  • 23. © Oscar Nierstrasz ST — Smalltalk Basics 2.23 Block closures > Use square brackets to delay evaluation of expressions ^ 1 < 2 ifTrue: ['smaller'] ifFalse: ['bigger'] 'smaller'
  • 24. © Oscar Nierstrasz ST — Smalltalk Basics 2.24 Variables > Local variables within methods (or blocks) are delimited by |var| > Block parameters are delimited by :var| OrderedCollection>>collect: aBlock "Evaluate aBlock with each of my elements as the argument." | newCollection | newCollection := self species new: self size. firstIndex to: lastIndex do: [ :index | newCollection addLast: (aBlock value: (array at: index))]. ^ newCollection [:n | |x y| x := n+1. y := n-1. x * y] value: 10 99
  • 25. © Oscar Nierstrasz ST — Smalltalk Basics 2.25 Control Structures > Every control structure is realized by message sends |n| n := 10. [n>0] whileTrue: [ Transcript show: n; cr. n := n-1 ] 1 to: 10 do: [:n| Transcript show: n; cr ] (1 to: 10) do: [:n| Transcript show: n; cr ]
  • 26. © Oscar Nierstrasz ST — Smalltalk Basics 2.26 Creating objects > Class methods > Factory methods OrderedCollection new Array with: 1 with: 2 1@2 "a Point" 1/2 "a Fraction"
  • 27. © Oscar Nierstrasz ST — Smalltalk Basics 2.27 Creating classes > Send a message to a class (!) Number subclass: #Complex instanceVariableNames: 'real imaginary' classVariableNames: '' poolDictionaries: '' category: 'ComplexNumbers'
  • 28. © Oscar Nierstrasz ST — Smalltalk Basics 2.28 Some Conventions > Method selector is a symbol, e.g., #add: > Method scope conventions using >> — Instance Method defined in the class Node — Class Method defined in the class Node class (i.e., in the class of the the class Node) > aSomething is an instance of the class Something Node>>accept: aPacket Node class>>withName: aSymbol
  • 29. © Oscar Nierstrasz ST — Smalltalk Basics 2.29 Roadmap > Everything is an Object > Syntax on a Postcard > Three Kinds of Messages > Methods, variables and blocks > Test-Driven Development > Managing Projects
  • 30. © Oscar Nierstrasz ST — Smalltalk Basics 2.30 Change sets > Make sure your changes are logged to a new change set
  • 31. © Oscar Nierstrasz ST — Smalltalk Basics 2.31 SUnit
  • 32. © Oscar Nierstrasz ST — Smalltalk Basics 2.32 Money TestCase subclass: #MoneyTest instanceVariableNames: 'chf2 chf8 chf10' classVariableNames: '' poolDictionaries: '' category: 'Money' > We will implement the Money example in Smalltalk — First, we develop a test case for a single currency NB: This is just a message sent to the TestCase class object (!)
  • 33. © Oscar Nierstrasz ST — Smalltalk Basics 2.33 SetUp > We will need setters for the private Money state MoneyTest>>setUp chf2 := Money new currency: 'CHF'; amount: 2. chf8 := Money new currency: 'CHF'; amount: 8. chf10 := Money new currency: 'CHF'; amount: 10.
  • 34. © Oscar Nierstrasz LECTURE TITLE 34 Protocols Classify methods into protocols that reveal their intent
  • 35. © Oscar Nierstrasz ST — Smalltalk Basics 2.35 MoneyTest>>testEquals > Some obvious tests MoneyTest>>testEquals self assert: chf2 = chf2. self assert: chf2 = (Money new currency: 'CHF'; amount: 2). self assert: chf2 != chf8.
  • 36. © Oscar Nierstrasz ST — Smalltalk Basics 2.36 Money > We define Money as a subclass of Object, with getters and setters Object subclass: #Money instanceVariableNames: 'currency amount' classVariableNames: '' poolDictionaries: '' category: 'Money' Money>>currency: aString currency := aString. Money>>currency ^ currency Money>>amount: aNumber amount:= aNumber. Money>>amount ^ amount
  • 37. © Oscar Nierstrasz ST — Smalltalk Basics 2.37 Failing tests
  • 38. © Oscar Nierstrasz ST — Smalltalk Basics 2.38 Comparisons Money>>= aMoney ^ self currency = aMoney currency and: [ self amount = aMoney amount ] Money>>!= aMoney ^ (self = aMoney) not
  • 39. © Oscar Nierstrasz ST — Smalltalk Basics 2.39 Constructors MoneyTest>>testEquals self assert: chf2 = chf2. self assert: chf2 = (Money currency: 'CHF' amount: 2). self assert: chf2 != chf8. We need a constructor on the class side of Money
  • 40. © Oscar Nierstrasz ST — Smalltalk Basics 2.40 Class methods NB: Which “self” is referred to in the method body?
  • 41. © Oscar Nierstrasz ST — Smalltalk Basics 2.41 Addition Money>>+ aMoney ^ Money currency: self currency amount: self amount + aMoney amount MoneyTest>>testAdd self assert: chf2 + chf8 = chf10 And so on …
  • 42. © Oscar Nierstrasz ST — Smalltalk Basics 2.42 Filing out your changes > You can “file out” all your changes so they can be loaded into another image
  • 43. © Oscar Nierstrasz ST — Smalltalk Basics 2.43 Change Sets 'From Pharo1.0beta of 16 May 2008 [Latest update: #10418] on 8 September 2009 at 1:58:51 pm'! Object subclass: #Money instanceVariableNames: 'currency amount' classVariableNames: '' poolDictionaries: '' category: 'Money'! TestCase subclass: #TestMoney instanceVariableNames: 'chf2 chf8 chf10' classVariableNames: '' poolDictionaries: '' category: 'Money'! !Money methodsFor: 'as yet unclassified' stamp: 'on 7/2/2007 13:21'! !!= aMoney ^ (self = aMoney) not! ! !Money methodsFor: 'as yet unclassified' stamp: 'on 7/2/2007 13:26'! + aMoney self assert: [ self currency = aMoney currency ]. ^ Money currency: self currency amount: self amount + aMoney amount! ! !Money methodsFor: 'as yet unclassified' stamp: 'on 7/2/2007 13:20'! = aMoney ^ self amount = aMoney amount and: [ self currency = aMoney currency ]! ! ...
  • 44. © Oscar Nierstrasz ST — Smalltalk Basics 2.44 Extensions > Creating new instances of Money is still cumbersome — We would like to be able to write instead code like this: TestMoney>>setUp chf2 := 2 chf. chf8 := 8 chf. chf10 := 10 chf.
  • 45. Extension protocols © Oscar Nierstrasz ST — Smalltalk Basics 2.45 To extend an existing class for a category MyProject, define the protocol *MyProject (or *myproject ) for that class.
  • 46. © Oscar Nierstrasz ST — Smalltalk Basics 2.46 Roadmap > Everything is an Object > Syntax on a Postcard > Three Kinds of Messages > Methods, variables and blocks > Test-Driven Development > Managing Projects
  • 47. © Oscar Nierstrasz ST — Smalltalk Basics 2.47 SqueakSource.com
  • 48. © Oscar Nierstrasz ST — Smalltalk Basics 2.48 Categories, Projects and Packages > A system category MyProject (and possibly MyProject-*) contains the classes of your application > A Monticello package MyProject contains the categories MyProject and MyProject-* — NB: If you have sub-categories, keep the top one empty! > A SqueakSource project MyProject stores everything in the Monticello package MyProject
  • 49. © Oscar Nierstrasz ST — Smalltalk Basics 2.49 Loading a project > To load a project from SqueakSource.com — Find the project in SqueakSource — Open the Monticello Browser — Add an HTTP Repository – Accept the code fragment from the SqueakSource page — Open the repository and load the latest version
  • 50. © Oscar Nierstrasz ST — Smalltalk Basics 2.50 Loading a project
  • 51. © Oscar Nierstrasz ST — Smalltalk Basics 2.51 Creating a project > To create a project on SqueakSource.com — Define your categories MyProject or MyProject-* — Create a SqueakSource project named MyProject — Define a Monticello package MyProject — Add an HTTP Repository for MyProject – Accept the code fragment from the SqueakSource page — Save the package
  • 52. © Oscar Nierstrasz ST — Smalltalk Basics 2.52 What you should know! How can you indicate that a method is “private”? What is the difference between a comment and a string? Why does 1+2*3 = 9? What is a cascade? How is a block like a lambda expression? How do you create a new class? How do you inspect an object?
  • 53. © Oscar Nierstrasz ST — Smalltalk Basics 2.53 Can you answer these questions? Why does Smalltalk support single (and not multiple) inheritance? Is the cascade strictly necessary? Why do you need to declare local variables if there are no static types? How can you discover the class of a GUI object? How does SUnit differ from JUnit?
  • 54. © Oscar Nierstrasz ST — Introduction 1.54 Attribution-ShareAlike 3.0 Unported You are free: to Share — to copy, distribute and transmit the work to Remix — to adapt the work Under the following conditions: Attribution. You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work). Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license. For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to this web page. Any of the above conditions can be waived if you get permission from the copyright holder. Nothing in this license impairs or restricts the author's moral rights. License http://creativecommons.org/licenses/by-sa/3.0/

Notes de l'éditeur

  1. NB: The name must match the package name; the title can be anything