A Little Journey in the Smalltalk...

Post on 01-Jun-2020

1 views 0 download

Transcript of A Little Journey in the Smalltalk...

Stéphane Ducasse

Stéphane Ducassestephane.ducasse@inria.frhttp://stephane.ducasse.free.fr/

!"#$

A Little Journey in the Smalltalk Syntax

1

S.Ducasse

!"#$

Goal

Lower your stress :)Show you that this is simple

2

S.Ducasse

!"#$

Appetizer!

3

S.Ducasse

!"#$

4

S.Ducasse

!"#$

Yeah!

Smalltalk is a dynamically typed language

5

S.Ducasse

!"#$

ArrayList<String> strings = new ArrayList<String>();

strings := ArrayList new.

6

S.Ducasse

!"#$

Shorter

Thread regThread = new Thread( new Runnable() { public void run() { this.doSomething();} }); regThread.start();

[self doSomething] fork.

7

S.Ducasse

!"#$

Smalltalk = Objects + Messages + (...)

8

S.Ducasse

RoadmapFun with numbers

9

S.Ducasse

!"#$

1 class

10

S.Ducasse

!"#$

1 class >SmallInteger

11

S.Ducasse

!"#$

1 class maxVal

12

S.Ducasse

!"#$

1 class maxVal>1073741823

13

S.Ducasse

!"#$

1 class maxVal + 1

14

S.Ducasse

!"#$

1 class maxVal + 1>1073741824

15

S.Ducasse

!"#$

(1 class maxVal + 1) class

16

S.Ducasse

!"#$

(1 class maxVal + 1) class>LargePositiveInteger

17

S.Ducasse

!"#$

(1/3) + (2/3)

18

S.Ducasse

!"#$

(1/3) + (2/3)>1

19

S.Ducasse

!"#$

2/3 + 1

20

S.Ducasse

!"#$

2/3 + 1> 5/3

21

S.Ducasse

!"#$

1000 factorial

22

S.Ducasse

!"#$

1000 factorial 402387260077093773543702433923003985719374864210714632543799910429938512398629020592044208486969404800479988610197196058631666872994808558901323829669944590997424504087073759918823627727188732519779505950995276120874975462497043601418278094646496291056393887437886487337119181045825783647849977012476632889835955735432513185323958463075557409114262417474349347553428646576611667797396668820291207379143853719588249808126867838374559731746136085379534524221586593201928090878297308431392844403281231558611036976801357304216168747609675871348312025478589320767169132448426236131412508780208000261683151027341827977704784635868170164365024153691398281264810213092761244896359928705114964975419909342221566832572080821333186116811553615836546984046708975602900950537616475847728421889679646244945160765353408198901385442487984959953319101723355556602139450399736280750137837615307127761926849034352625200015888535147331611702103968175921510907788019393178114194545257223865541461062892187960223838971476088506276862967146674697562911234082439208160153780889893964518263243671616762179168909779911903754031274622289988005195444414282012187361745992642956581746628302955570299024324153181617210465832036786906117260158783520751516284225540265170483304226143974286933061690897968482590125458327168226458066526769958652682272807075781391858178889652208164348344825993266043367660176999612831860788386150279465955131156552036093988180612138558600301435694527224206344631797460594682573103790084024432438465657245014402821885252470935190620929023136493273497565513958720559654228749774011413346962715422845862377387538230483865688976461927383814900140767310446640259899490222221765904339901886018566526485061799702356193897017860040811889729918311021171229845901641921068884387121855646124960798722908519296819372388642614839657382291123125024186649353143970137428531926649875337218940694281434118520158014123344828015051399694290153483077644569099073152433278288269864602789864321139083506217095002597389863554277196742822248757586765752344220207573630569498825087968928162753848863396909959826280956121450994871701244516461260379029309120889086942028510640182154399457156805941872748998094254742173582401063677404595741785160829230135358081840096996372524230560855903700624271243416909004153690105933983835777939410970027753472000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

23

S.Ducasse

!"#$

1000 factorial / 999 factorial

24

S.Ducasse

!"#$

1000 factorial / 999 factorial> 1000

25

S.Ducasse

!"#$

10 @ 100

26

S.Ducasse

!"#$

10 @ 100

(10 @ 100) x

27

S.Ducasse

!"#$

10 @ 100

(10 @ 100) x> 10

28

S.Ducasse

!"#$

10 @ 100

(10 @ 100) x> 10

(10 @ 100) y

29

S.Ducasse

!"#$

10 @ 100

(10 @ 100) x> 10

(10 @ 100) y>100

30

S.Ducasse

!"#$

Points!

Points are created using @

31

S.Ducasse

!"#$

Puzzle

(10@100) + (20@100)

32

S.Ducasse

!"#$

Puzzle

(10@100) + (20@100) >30@200

33

S.Ducasse

!"#$

Puzzle

(10@100) + (20@100) >30@200

34

S.Ducasse

RoadmapFun with characters, strings, arrays

35

S.Ducasse

!"#$

$C $h $a $r $a $c $t $e $r

$F $Q $U $E $N $T $i $N

36

S.Ducasse

!"#$

space? tab?

Character spaceCharacter tabCharacter cr

37

S.Ducasse

!"#$

'Strings'

'Tiramisu'

38

S.Ducasse

!"#$

Characters

12 printString

> '12'

39

S.Ducasse

!"#$

Strings are collections of chars

'Tiramisu' at: 1

40

S.Ducasse

!"#$

Strings are collections of chars

'Tiramisu' at: 1

> $T

41

S.Ducasse

!"#$

A program — Finding the last character

42

S.Ducasse

!"#$

A program!

| str |

43

S.Ducasse

!"#$

A program!

| str | local variable

44

S.Ducasse

!"#$

A program!

| str | local variablestr := 'Tiramisu'.

45

S.Ducasse

!"#$

A program!

| str | local variablestr := 'Tiramisu'. assignment

46

S.Ducasse

!"#$

A program!

| str | local variablestr := 'Tiramisu'. assignmentstr at: str size

47

S.Ducasse

!"#$

A program!

| str | local variablestr := 'Tiramisu'. assignmentstr at: str size message send

> $u

48

S.Ducasse

!"#$

double ' to get one

'L''Idiot'

> one string, containing a single apostrophe

49

S.Ducasse

!"#$

For concatenation use ,

'Calvin' , ' & ', 'Hobbes'

50

S.Ducasse

!"#$

For concatenation use ,

'Calvin' , ' & ', 'Hobbes'> 'Clavin & Hobbes'

51

S.Ducasse

!"#$

For concatenation use ,

'Calvin' , ' & ', 'Hobbes'> 'Calvin & Hobbes'

52

S.Ducasse

!"#$

Symbols: #Pharo

#Something is a symbol

Symbol is a unique string in the system

#Something == #Something > true

53

S.Ducasse

!"#$

“Comment”

“what a fun language lecture.I really liked the desserts”

54

S.Ducasse

!"#$

#(Array)

#('Calvin' 'hates' 'Suzie')

55

S.Ducasse

!"#$

#(Array)

#('Calvin' 'hates' 'Suzie') size

56

S.Ducasse

!"#$

#(Array)

#('Calvin' 'hates' 'Suzie') size> 3

57

S.Ducasse

!"#$

First element starts at 1

#('Calvin' 'hates' 'Suzie') at: 2

58

S.Ducasse

!"#$

First element starts at 1

#('Calvin' 'hates' 'Suzie') at: 2> 'hates'

59

S.Ducasse

!"#$

at: to access, at:put: to set

#('Calvin' 'hates' 'Suzie') at: 2 put: 'loves'

60

S.Ducasse

!"#$

#(Array)

#('Calvin' 'hates' 'Suzie') at: 2 put: 'loves'

> #('Calvin' 'loves' 'Suzie')

61

S.Ducasse

Syntax Summarycomment: "a comment"character: $c $h $a $r $a $c $t $e $r $s $# $@ string: 'a nice string' 'lulu' 'l''idiot'symbol: #mac #+ array: #(1 2 3 (1 3) $a 4)byte array: #[1 2 3]integer: 1 2r101real: 1.5 6.03e-34 4.0 2.4e7fraction: 1/33boolean: true falsepoint: 10@120

62

S.Ducasse

RoadmapFun with keywords-based messages

63

S.Ducasse

!"#$

Keyword-based messages

arr at: 2 put: 'loves'

64

S.Ducasse

!"#$

Keyword-based messages

arr at: 2 put: 'loves'

somehow like arr.atput(2,'loves')

65

S.Ducasse

!"#$

From Java to Smalltalk

postman.send(mail,recipient);

66

S.Ducasse

!"#$

Removing

postman.send(mail,recipient);

67

S.Ducasse

!"#$

Removing unnecessary

postman send mail recipient

68

S.Ducasse

!"#$

But without losing information

postman send mail to recipient

69

S.Ducasse

!"#$

postman send: mail to: recipient

postman.send(mail,recipient);

70

S.Ducasse

!"#$

postman send: mail to: recipient

The message is send:to:

postman.send(mail,recipient);

71

S.Ducasse

RoadmapFun with variables

72

S.Ducasse

!"#$

Shared or Global starts with Uppercase

Transcript cr .Transcript show: 'hello world'.Transcript cr .

73

S.Ducasse

!"#$

local or temps starts with lowercase

| str |str := 'Tiramisu'

74

S.Ducasse

!"#$

self, super, true, false, nil

self = thissuper

true, false are for Booleans

nil is UndefinedObject instance

75

S.Ducasse

!"#$

self, super, true, false, nil

self = this in Javasuper

true, false are for Booleans

nil is UndefinedObject instance

76

S.Ducasse

RoadmapFun with classes

77

S.Ducasse

!"#$

A class definition!

Superclass subclass: #Class instanceVariableNames: 'a b c'

... category: 'Package name'

78

S.Ducasse

!"#$

A class definition!

Object subclass: #Point instanceVariableNames: 'x y' classVariableNames: '' poolDictionaries: '' category: 'Graphics-Primitives'

79

S.Ducasse

!"#$

A class definition!

Object subclass: #Point instanceVariableNames: 'x y' classVariableNames: '' poolDictionaries: '' category: 'Graphics-Primitives'

80

S.Ducasse

RoadmapFun with methods

81

S.Ducasse

!"#$

On Integer

asComplex"Answer a Complex number that represents value of the the receiver."

^ Complex real: self imaginary: 0

82

S.Ducasse

!"#$

On Boolean

xor: aBoolean "Exclusive OR. Answer true if the receiver is not equivalent to aBoolean."

^(self == aBoolean) not

83

S.Ducasse

!"#$

Summary

self, supercan access instance variablescan define local variable | ... |Do not need to define argument types^ to return

84

S.Ducasse

RoadmapFun with unary messages

85

S.Ducasse

!"#$

1 class

86

S.Ducasse

!"#$

1 class> SmallInteger

87

S.Ducasse

!"#$

false not

88

S.Ducasse

!"#$

false not> true

89

S.Ducasse

!"#$

Date today

90

S.Ducasse

!"#$

Date today> 24 May 2009

91

S.Ducasse

!"#$

Time now

92

S.Ducasse

!"#$

Time now> 6:50:03 pm

93

S.Ducasse

!"#$

Float pi

94

S.Ducasse

!"#$

Float pi> 3.141592653589793

95

S.Ducasse

!"#$

We send messages to both objects and to classes!

1 classDate today

96

S.Ducasse

!"#$

We sent messages to objects or classes!

1 classDate today

97

S.Ducasse

Fun with binary messages

98

Roadmap

S.Ducasse

!"#$

aReceiver aSelector anArgument

Used for arithmetic, comparison and logical operationsOne or two characters taken from:

+ - / \ * ~ < > = @ % | & ! ? ,

99

S.Ducasse

!"#$

1 + 2

100

S.Ducasse

!"#$

1 + 2>3

101

S.Ducasse

!"#$

2 => 3

102

S.Ducasse

!"#$

2 => 3> false

103

S.Ducasse

!"#$

10 @ 200

104

S.Ducasse

!"#$

'Black chocolate' , ' is good'

105

S.Ducasse

RoadmapFun with keyword-based messages

106

S.Ducasse

!"#$

#('Calvin' 'hates' 'Suzie') at: 2 put: 'loves'

107

S.Ducasse

!"#$

#('Calvin' 'hates' 'Suzie') at: 2 put: 'loves'> #('Calvin' 'loves' 'Suzie')

108

S.Ducasse

!"#$

10@20 setX: 2

109

S.Ducasse

!"#$

10@20 setX: 2> 2@20

110

S.Ducasse

!"#$

12 between: 10 and: 20

111

S.Ducasse

!"#$

12 between: 10 and: 20> true

112

S.Ducasse

!"#$

receiver keyword1: argument1

keyword2: argument2

equivalent to receiver.keyword1keyword2(argument1, argument2)

113

S.Ducasse

!"#$

receiver keyword1: argument1

keyword2: argument2

equivalent to receiver.keyword1keyword2(argument1, argument2)

114

S.Ducasse

Browser newOnClass: Point

115

Roadmap

S.Ducasse

!"#$

Browser newOnClass: Point

116

S.Ducasse

!"#$

Browser newOnClass: Point

117

S.Ducasse

!"#$

Yes there is a difference between doing (side effect)and returning an object

118

S.Ducasse

!"#$

Browser newOnClass: Point> a Browser

119

S.Ducasse

!"#$

Doing when you do not care about the answer:

Browser newOnClass: Point

120

S.Ducasse

!"#$

Doing when you really want to see the answer:

10@20 setX: 2> 2@20

121

This is called printing

S.Ducasse

!"#$

doIt (do some Smalltalk code) vs printIt (doIt + printing              the answer)

122

S.Ducasse

!"#$

123

doIt (do some Smalltalk code) vs printIt (doIt + printing              the answer)

S.Ducasse

RoadmapMessages messagesmessagesagain messages....

124

S.Ducasse

!"#$

Yes, there are only messages unary binary keywords

125

S.Ducasse

!"#$

Composition: from left to right!

69 class inspect

69 class superclass superclass inspect

126

S.Ducasse

!"#$

Unary> Binary> Keywords

127

Precedence

S.Ducasse

!"#$

2 + 3 squared

128

S.Ducasse

!"#$

2 + 3 squared> 2 + 9

129

S.Ducasse

!"#$

2 + 3 squared> 2 + 9> 11

130

S.Ducasse

!"#$

Color gray - Color white = Color black

131

S.Ducasse

!"#$

Color gray - Color white = Color black> aColor = Color black

132

S.Ducasse

!"#$

Color gray - Color white = Color black> aColor = Color black> true

133

S.Ducasse

!"#$

2 raisedTo: 3 + 2

134

S.Ducasse

!"#$

2 raisedTo: 3 + 2> 2 raisedTo: 5

135

S.Ducasse

!"#$

2 raisedTo: 3 + 2> 2 raisedTo: 5> 32

136

S.Ducasse

!"#$

1/3 + 2/3

137

No mathematical precedence

S.Ducasse

!"#$

1/3 + 2/3>7/3 /3

138

No mathematical precedence

S.Ducasse

!"#$

Parenthesized takes precedence!

139

(Msg) > Unary> Binary> Keywords

S.Ducasse

!"#$

(0@0 extent: 100@100) bottomRight

140

S.Ducasse

!"#$

(0@0 extent: 100@100) bottomRight> (aPoint extent: anotherPoint) bottomRight

141

S.Ducasse

!"#$

(0@0 extent: 100@100) bottomRight> (aPoint extent: anotherPoint) bottomRight> aRectangle bottomRight

142

S.Ducasse

!"#$

(0@0 extent: 100@100) bottomRight> (aPoint extent: anotherPoint) bottomRight> aRectangle bottomRight> 100@100

143

S.Ducasse

!"#$

0@0 extent: 100@100 bottomRight

144

S.Ducasse

!"#$

0@0 extent: 100@100 bottomRight> Message not understood> 100 does not understand bottomRight

145

S.Ducasse

!"#$

3 + 2 * 10

146

No mathematical precedence

S.Ducasse

!"#$

3 + 2 * 10> 5 * 10

147

No mathematical precedence

S.Ducasse

!"#$

3 + 2 * 10> 5 * 10> 50 argh!

148

No mathematical precedence

S.Ducasse

!"#$

3 + (2 * 10)

149

No mathematical precedence

S.Ducasse

!"#$

3 + (2 * 10)> 3 + 20

150

No mathematical precedence

S.Ducasse

!"#$

3 + (2 * 10)> 3 + 20> 23

151

No mathematical precedence

S.Ducasse

!"#$

1/3 + 2/3 > 7/3 /3

152

No mathematical precedence

S.Ducasse

!"#$

1/3 + 2/3 > (7/3) /3> 7/9

153

No mathematical precedence

S.Ducasse

!"#$

(1/3) + (2/3)

154

No mathematical precedence

S.Ducasse

!"#$

(1/3) + (2/3)> 1

155

No mathematical precedence

S.Ducasse

!"#$

(Msg) > Unary > Binary > Keywords from left to rightNo mathematical precedence

156

Only Messages

S.Ducasse

!"#$

(Msg) > Unary > Binary > Keywords from left to rightNo mathematical precedence

157

Only Messages

S.Ducasse

RoadmapFun with blocks

158

S.Ducasse

!"#$

Function definition

fct(x) = x * x + x

159

S.Ducasse

!"#$

Function Application

fct (2) = 6fct (20) = 420

160

S.Ducasse

!"#$

Function definition

fct(x) = x * x + x

|fct|fct:= [:x | x * x + x].

161

S.Ducasse

!"#$

Function Application

fct (2) = 6fct (20) = 420

fct value: 2 > 6fct value: 20 > 420

162

S.Ducasse

!"#$

Other examples

[2 + 3 + 4 + 5] value[:x | x + 3 + 4 + 5 ] value: 2[:x :y | x + y + 4 + 5] value: 2 value: 3

163

S.Ducasse

!"#$

Block

anonymous function

[ :variable1 :variable2 | | tmp | expression1. ...variable1 ... ]

To execute the function, use the

    value: ... value: ...

message

164

S.Ducasse

!"#$

Block

anonymous function — represented as an objectReally really cool!Can be passed to methods, stored in instance variables ...

[ :variable1 :variable2 | | tmp | expression1. ...variable1 ... ]

value: ... value: ...

165

S.Ducasse

RoadmapFun with conditional

166

S.Ducasse

!"#$

Example

3 > 0 ifTrue:['positive'] ifFalse:['negative']

167

S.Ducasse

!"#$

Example

3 > 0 ifTrue:['positive'] ifFalse:['negative']

> 'positive'

168

S.Ducasse

!"#$

Yes ifTrue:ifFalse: is a message!

Weather isRaining ifTrue: [self takeMyUmbrella] ifFalse: [self takeMySunglasses]

ifTrue:ifFalse is sent to an object: a boolean!

169

S.Ducasse

!"#$

Booleans

& | notor: and: (lazy)xor:ifTrue:ifFalse:ifFalse:ifTrue:...

170

S.Ducasse

!"#$

Yes! ifTrue:ifFalse: is a message send to a Boolean.

But optimized by the compiler :)

171

S.Ducasse

!"#$

10 timesRepeat: [ Transcript show: 'hello'; cr]

172

S.Ducasse

!"#$

10 timesRepeat: [ Transcript show: 'hello'; cr]

173

S.Ducasse

!"#$

[x<y] whileTrue: [x := x + 3]

174

S.Ducasse

!"#$

aBlockTest whileTrueaBlockTest whileFalseaBlockTest whileTrue: aBlockBodyaBlockTest whileFalse: aBlockBodyanInteger timesRepeat: aBlockBody

175

S.Ducasse

!"#$

Confused about () and [] ?

176

S.Ducasse

!"#$

Use [ ] when you do not know the number of times something may be executed

(x isBlue) ifTrue: [ x schroumph ]

n timesRepeat: [ self shout ]

177

S.Ducasse

!"#$

Control statement of other languages are replaced by messages sent to booleans:

(x isBlue) ifTrue: [ ]

178

S.Ducasse

Fun with loops

179

Roadmap

S.Ducasse

!"#$

1 to: 100 do: [ :i | Transcript show: i ; space]

180

S.Ducasse

!"#$

1 to: 100 do: [ :i | Transcript show: i ; space]

181

S.Ducasse

!"#$

1 to: 100 by: 3 do: [ :i | Transcript show: i ; space]

182

S.Ducasse

!"#$

1 to: 100 by: 3 do: [ :i | Transcript show: i ; space]

183

S.Ducasse

!"#$

So: yes, there are real loops in Smalltalk!

to:do: to:by:do:

are just messages send to integers

184

S.Ducasse

!"#$

185

So: yes, there are real loops in Smalltalk!

to:do: to:by:do:

are just messages send to integers

S.Ducasse

RoadmapFun with iterators

186

S.Ducasse

!"#$

ArrayList<String> strings = new ArrayList<String>(); for(Person person: persons) strings.add(person.name());

strings := persons collect [:person | person name].

187

S.Ducasse

!"#$

#(2 -3 4 -35 4) collect: [ :each| each abs]

188

S.Ducasse

!"#$

#(2 -3 4 -35 4) collect: [ :each| each abs]> #(2 3 4 35 4)

189

S.Ducasse

!"#$

#(15 10 19 68) collect: [:i | i odd ]

190

S.Ducasse

!"#$

#(15 10 19 68) collect: [:i | i odd ]> #(true false true false)

191

S.Ducasse

!"#$

#(15 10 19 68) collect: [:i | i odd ]

We can also do it this way:

|result|aCol := #( 2 -3 4 -35 4).result := aCol species new: aCol size.1 to: aCollection size do: [ :each | result at: each put: (aCol at: each) odd].result

192

S.Ducasse

!"#$

193

#(15 10 19 68) collect: [:i | i odd ]

We can also do it this way:

|result|aCol := #( 2 -3 4 -35 4).result := aCol species new: aCol size.1 to: aCollection size do: [ :each | result at: each put: (aCol at: each) odd].result

But don't!

S.Ducasse

!"#$

#(15 10 19 68) do: [:i | Transcript show: i ; cr ]

194

S.Ducasse

!"#$

#(15 10 19 68) do: [:i | Transcript show: i ; cr ]

195

S.Ducasse

!"#$

#(1 2 3) with: #(10 20 30) do: [:x :y| Transcript show: (y * x) ; cr ]

196

S.Ducasse

!"#$

#(1 2 3) with: #(10 20 30) do: [:x :y| Transcript show: (y * x) ; cr ]

197

S.Ducasse

!"#$

How is do: implemented?

198

S.Ducasse

!"#$

How do: is implemented?

SequenceableCollection ›› do: aBlock "Evaluate aBlock with each of the receiver's elements as the argument."

1 to: self size do: [:i | aBlock value: (self at: i)]

199

S.Ducasse

!"#$

Some others... make them your friends

#(15 10 19 68) select: [:i|i odd]

#(15 10 19 68) reject: [:i|i odd]

#(12 10 19 68 21) detect: [:i|i odd]

#(12 10 12 68) detect: [:i|i odd] ifNone:[1]

200

S.Ducasse

!"#$

#(15 10 19 68) select: [:i|i odd] > #(15 19)

#(15 10 19 68) reject: [:i|i odd]

#(12 10 19 68 21) detect: [:i|i odd]

#(12 10 12 68) detect: [:i|i odd] ifNone:[1]

201

Some others... make them your friends

S.Ducasse

!"#$

#(15 10 19 68) select: [:i|i odd] > #(15 19)

#(15 10 19 68) reject: [:i|i odd] > #(10 68)

#(12 10 19 68 21) detect: [:i|i odd]

#(12 10 12 68) detect: [:i|i odd] ifNone:[1]

202

Some others... make them your friends

S.Ducasse

!"#$

#(15 10 19 68) select: [:i|i odd] > #(15 19)

#(15 10 19 68) reject: [:i|i odd] > #(10 68)

#(12 10 19 68 21) detect: [:i|i odd] > 19

#(12 10 12 68) detect: [:i|i odd] ifNone:[1]

203

Some others... make them your friends

S.Ducasse

!"#$

#(15 10 19 68) select: [:i|i odd] > #(15 19)

#(15 10 19 68) reject: [:i|i odd] > #(10 68)

#(12 10 19 68 21) detect: [:i|i odd] > 19

#(12 10 12 68) detect: [:i|i odd] ifNone:[1]> 1

204

Some others... make them your friends

S.Ducasse

!"#$

Iterators are your best friends compact nice abstraction    puts the code with the data structure Just messages sent to collections

205

S.Ducasse

!"#$

206

Iterators are your best friends compact nice abstraction    puts the code with the data structure Just messages sent to collections

S.Ducasse

!"#$

A simple exercise

How do you write a method that does this?

#() ⧴ (nothing) #(a) ⧴ a #(a b c) ⧴ a, b, c

207

S.Ducasse

!"#$

#(a b c)do: [:each | Transcript show: each]separatedBy: [Transcript show: ', ']

208

S.Ducasse

!"#$

209

#(a b c)do: [:each | Transcript show: each]separatedBy: [Transcript show: ', ']

S.Ducasse

More fun with Messages

210

S.Ducasse

!"#$

Messages Sequence message1 . message2 . message3 . is a separator, not a terminator | macNode pcNode node1 printerNode | macNode := Workstation withName: #mac. Transcript cr. Transcript show: 1 printString. Transcript cr. Transcript show: 2 printString

211

S.Ducasse

!"#$

Multiple messages to one object ;

To send multiple messages to the same object, use ;

Transcript show: 3 printString; cr

is equivalent to:

Transcript show: 3 printString. Transcript cr

212

S.Ducasse

!"#$

Hints ...

x isNil ifTrue: [...]

better:

x ifNil: [...]

213

S.Ducasse

!"#$

Hints ...

x includes: 3 ifTrue: [...]

is read as sending the message includes:ifTrue:

So: use parenthesis

(x includes: 3) ifTrue: [...]

214

S.Ducasse

!"#$

215

S.Ducasse

Smalltalk is funPure, simple, powerful

www.seaside.stwww.dabbledb.comwww.pharo-project.org

216