8 - oop - smalltalk syntax

214
Stéphane Ducasse 1 Stéphane Ducasse [email protected] http://stephane.ducasse.fre e.fr/ A Little Journey in the Smalltalk Syntax

Upload: the-world-of-smalltalk

Post on 23-Dec-2014

549 views

Category:

Education


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: 8 - OOP - Smalltalk Syntax

Stéphane Ducasse 1

Stéphane [email protected]://stephane.ducasse.free.fr/

A Little Journey in the Smalltalk Syntax

Page 2: 8 - OOP - Smalltalk Syntax

S.Ducasse 2

Goal

Lower your stress :)Show you that this is simple

Page 3: 8 - OOP - Smalltalk Syntax

S.Ducasse 3

Appetizer!

Page 4: 8 - OOP - Smalltalk Syntax

S.Ducasse 4

Page 5: 8 - OOP - Smalltalk Syntax

S.Ducasse 5

Yeah!

Smalltalk is a dynamically typed language

Page 6: 8 - OOP - Smalltalk Syntax

S.Ducasse 6

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

strings := ArrayList new.

Page 7: 8 - OOP - Smalltalk Syntax

S.Ducasse 7

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

[self doSomething] fork.

Page 8: 8 - OOP - Smalltalk Syntax

S.Ducasse 8

Smalltalk = Objects + Messages + (...)

Page 9: 8 - OOP - Smalltalk Syntax

S.Ducasse 9

RoadmapFun with numbers

Page 10: 8 - OOP - Smalltalk Syntax

S.Ducasse 10

1 class

Page 11: 8 - OOP - Smalltalk Syntax

S.Ducasse 11

1 class >SmallInteger

Page 12: 8 - OOP - Smalltalk Syntax

S.Ducasse 12

1 class maxVal

Page 13: 8 - OOP - Smalltalk Syntax

S.Ducasse 13

1 class maxVal>1073741823

Page 14: 8 - OOP - Smalltalk Syntax

S.Ducasse 14

1 class maxVal + 1

Page 15: 8 - OOP - Smalltalk Syntax

S.Ducasse 15

1 class maxVal + 1>1073741824

Page 16: 8 - OOP - Smalltalk Syntax

S.Ducasse 16

(1 class maxVal + 1) class

Page 17: 8 - OOP - Smalltalk Syntax

S.Ducasse 17

(1 class maxVal + 1) class>LargePositiveInteger

Page 18: 8 - OOP - Smalltalk Syntax

S.Ducasse 18

(1/3) + (2/3)

Page 19: 8 - OOP - Smalltalk Syntax

S.Ducasse 19

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

Page 20: 8 - OOP - Smalltalk Syntax

S.Ducasse 20

2/3 + 1

Page 21: 8 - OOP - Smalltalk Syntax

S.Ducasse 21

2/3 + 1> 5/3

Page 22: 8 - OOP - Smalltalk Syntax

S.Ducasse 22

1000 factorial

Page 23: 8 - OOP - Smalltalk Syntax

S.Ducasse 23

1000 factorial 402387260077093773543702433923003985719374864210714632543799910429938512398629020592044208486969404800479988610197196058631666872994808558901323829669944590997424504087073759918823627727188732519779505950995276120874975462497043601418278094646496291056393887437886487337119181045825783647849977012476632889835955735432513185323958463075557409114262417474349347553428646576611667797396668820291207379143853719588249808126867838374559731746136085379534524221586593201928090878297308431392844403281231558611036976801357304216168747609675871348312025478589320767169132448426236131412508780208000261683151027341827977704784635868170164365024153691398281264810213092761244896359928705114964975419909342221566832572080821333186116811553615836546984046708975602900950537616475847728421889679646244945160765353408198901385442487984959953319101723355556602139450399736280750137837615307127761926849034352625200015888535147331611702103968175921510907788019393178114194545257223865541461062892187960223838971476088506276862967146674697562911234082439208160153780889893964518263243671616762179168909779911903754031274622289988005195444414282012187361745992642956581746628302955570299024324153181617210465832036786906117260158783520751516284225540265170483304226143974286933061690897968482590125458327168226458066526769958652682272807075781391858178889652208164348344825993266043367660176999612831860788386150279465955131156552036093988180612138558600301435694527224206344631797460594682573103790084024432438465657245014402821885252470935190620929023136493273497565513958720559654228749774011413346962715422845862377387538230483865688976461927383814900140767310446640259899490222221765904339901886018566526485061799702356193897017860040811889729918311021171229845901641921068884387121855646124960798722908519296819372388642614839657382291123125024186649353143970137428531926649875337218940694281434118520158014123344828015051399694290153483077644569099073152433278288269864602789864321139083506217095002597389863554277196742822248757586765752344220207573630569498825087968928162753848863396909959826280956121450994871701244516461260379029309120889086942028510640182154399457156805941872748998094254742173582401063677404595741785160829230135358081840096996372524230560855903700624271243416909004153690105933983835777939410970027753472000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

Page 24: 8 - OOP - Smalltalk Syntax

S.Ducasse 24

1000 factorial / 999 factorial

Page 25: 8 - OOP - Smalltalk Syntax

S.Ducasse 25

1000 factorial / 999 factorial> 1000

Page 26: 8 - OOP - Smalltalk Syntax

S.Ducasse 26

10 @ 100

Page 27: 8 - OOP - Smalltalk Syntax

S.Ducasse 27

10 @ 100

(10 @ 100) x

Page 28: 8 - OOP - Smalltalk Syntax

S.Ducasse 28

10 @ 100

(10 @ 100) x> 10

Page 29: 8 - OOP - Smalltalk Syntax

S.Ducasse 29

10 @ 100

(10 @ 100) x> 10

(10 @ 100) y

Page 30: 8 - OOP - Smalltalk Syntax

S.Ducasse 30

10 @ 100

(10 @ 100) x> 10

(10 @ 100) y>100

Page 31: 8 - OOP - Smalltalk Syntax

S.Ducasse 31

Points!

Points are created using @

Page 32: 8 - OOP - Smalltalk Syntax

S.Ducasse 32

Puzzle

(10@100) + (20@100)

Page 33: 8 - OOP - Smalltalk Syntax

S.Ducasse 33

Puzzle

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

Page 34: 8 - OOP - Smalltalk Syntax

S.Ducasse 34

Puzzle

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

Page 35: 8 - OOP - Smalltalk Syntax

S.Ducasse 35

RoadmapFun with characters, strings, arrays

Page 36: 8 - OOP - Smalltalk Syntax

S.Ducasse 36

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

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

Page 37: 8 - OOP - Smalltalk Syntax

S.Ducasse 37

space tab?!

Character spaceCharacter tabCharacter cr

Page 38: 8 - OOP - Smalltalk Syntax

S.Ducasse 38

‘Strings’

‘Tiramisu’

Page 39: 8 - OOP - Smalltalk Syntax

S.Ducasse 39

Characters

12 printString

> ’12’

Page 40: 8 - OOP - Smalltalk Syntax

S.Ducasse 40

Strings are collections of chars

‘Tiramisu’ at: 1

Page 41: 8 - OOP - Smalltalk Syntax

S.Ducasse 41

Strings are collections of chars

‘Tiramisu’ at: 1

> $T

Page 42: 8 - OOP - Smalltalk Syntax

S.Ducasse 42

A program! -- finding the last char

Page 43: 8 - OOP - Smalltalk Syntax

S.Ducasse 43

A program!

| str |

Page 44: 8 - OOP - Smalltalk Syntax

S.Ducasse 44

A program!

| str | local variable

Page 45: 8 - OOP - Smalltalk Syntax

S.Ducasse 45

A program!

| str | local variablestr := ‘Tiramisu’.

Page 46: 8 - OOP - Smalltalk Syntax

S.Ducasse 46

A program!

| str | local variablestr := ‘Tiramisu’. assignment

Page 47: 8 - OOP - Smalltalk Syntax

S.Ducasse 47

A program!

| str | local variablestr := ‘Tiramisu’. assignmentstr at: str length

Page 48: 8 - OOP - Smalltalk Syntax

S.Ducasse 48

A program!

| str | local variablestr := ‘Tiramisu’. assignmentstr at: str length message send

> $u

Page 49: 8 - OOP - Smalltalk Syntax

S.Ducasse 49

double ‘ to get one

‘L’’Idiot’

> one string

Page 50: 8 - OOP - Smalltalk Syntax

S.Ducasse 50

For concatenation use ,

‘Calvin’ , ‘ & ‘, ‘Hobbes’

Page 51: 8 - OOP - Smalltalk Syntax

S.Ducasse 51

For concatenation use ,

‘Calvin’ , ‘ & ‘, ‘Hobbes’> ‘Calvin & Hobbes’

Page 52: 8 - OOP - Smalltalk Syntax

S.Ducasse 52

For concatenation use ,

‘Calvin’ , ‘ & ‘, ‘Hobbes’> ‘Calvin & Hobbes’

Page 53: 8 - OOP - Smalltalk Syntax

S.Ducasse 53

Symbols: #Pharo

#Something is a symbol

Symbol is a unique string in the system

#Something == #Something > true

Page 54: 8 - OOP - Smalltalk Syntax

S.Ducasse 54

“Comment”

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

Page 55: 8 - OOP - Smalltalk Syntax

S.Ducasse 55

#(Array)

#(‘Calvin’ ‘hates’ ‘Suzie’)

Page 56: 8 - OOP - Smalltalk Syntax

S.Ducasse 56

#(Array)

#(‘Calvin’ ‘hates’ ‘Suzie’) size

Page 57: 8 - OOP - Smalltalk Syntax

S.Ducasse 57

#(Array)

#(‘Calvin’ ‘hates’ ‘Suzie’) size> 3

Page 58: 8 - OOP - Smalltalk Syntax

S.Ducasse 58

First element starts at 1

#(‘Calvin’ ‘hates’ ‘Suzie’) at: 2

Page 59: 8 - OOP - Smalltalk Syntax

S.Ducasse 59

First element starts at 1

#(‘Calvin’ ‘hates’ ‘Suzie’) at: 2> ‘hates’

Page 60: 8 - OOP - Smalltalk Syntax

S.Ducasse 60

at: to access, at:put: to set

#(‘Calvin’ ‘hates’ ‘Suzie’) at: 2 put: ‘loves’

Page 61: 8 - OOP - Smalltalk Syntax

S.Ducasse 61

#(Array)

#(‘Calvin’ ‘hates’ ‘Suzie’) at: 2 put: ‘loves’

> #(‘Calvin’ ‘loves’ ‘Suzie’)

Page 62: 8 - OOP - Smalltalk Syntax

S.Ducasse 62

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, 2.4e7fraction: 1/33boolean: true, falsepoint: 10@120

Page 63: 8 - OOP - Smalltalk Syntax

S.Ducasse 63

RoadmapFun with keywords-based messages

Page 64: 8 - OOP - Smalltalk Syntax

S.Ducasse 64

Keyword-based messages

arr at: 2 put: ‘loves’

Page 65: 8 - OOP - Smalltalk Syntax

S.Ducasse 65

Keyword-based messages

arr at: 2 put: ‘loves’

somehow like arr.atput(2,‘loves’)

Page 66: 8 - OOP - Smalltalk Syntax

S.Ducasse 66

From Java to Smalltalk

postman.send(mail,recipient);

Page 67: 8 - OOP - Smalltalk Syntax

S.Ducasse 67

Removing

postman.send(mail,recipient);

Page 68: 8 - OOP - Smalltalk Syntax

S.Ducasse 68

Removing unnecessary

postman send mail recipient

Page 69: 8 - OOP - Smalltalk Syntax

S.Ducasse 69

But without losing information

postman send mail to recipient

Page 70: 8 - OOP - Smalltalk Syntax

S.Ducasse 70

postman send: mail to: recipient

postman.send(mail,recipient);

Page 71: 8 - OOP - Smalltalk Syntax

S.Ducasse 71

postman send: mail to: recipient

The message is send:to:

postman.send(mail,recipient);

Page 72: 8 - OOP - Smalltalk Syntax

S.Ducasse 72

RoadmapFun with variables

Page 73: 8 - OOP - Smalltalk Syntax

S.Ducasse 73

Shared or Global starts with Uppercase

Transcript cr .Transcript show: ‘hello world’.Transcript cr .

Page 74: 8 - OOP - Smalltalk Syntax

S.Ducasse 74

local or temps starts with lowercase

| str |str := ‘Tiramisu’

Page 75: 8 - OOP - Smalltalk Syntax

S.Ducasse 75

self, super, true, false, nil

self = thissuper

true, false are for Booleans

nil is UndefinedObject instance

Page 76: 8 - OOP - Smalltalk Syntax

S.Ducasse 76

self, super, true, false, nil

self = this in Javasuper

true, false are for Booleans

nil is UndefinedObject instance

Page 77: 8 - OOP - Smalltalk Syntax

S.Ducasse 77

RoadmapFun with classes

Page 78: 8 - OOP - Smalltalk Syntax

S.Ducasse 78

A class definition!

Superclass subclass: #ClassinstanceVariableNames: 'a b c'

...category: 'Package name'

Page 79: 8 - OOP - Smalltalk Syntax

S.Ducasse 79

A class definition!

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

Page 80: 8 - OOP - Smalltalk Syntax

S.Ducasse 80

A class definition!

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

Page 81: 8 - OOP - Smalltalk Syntax

S.Ducasse 81

RoadmapFun with methods

Page 82: 8 - OOP - Smalltalk Syntax

S.Ducasse 82

On Integer

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

^ Complex real: self imaginary: 0

Page 83: 8 - OOP - Smalltalk Syntax

S.Ducasse 83

On Boolean

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

^(self == aBoolean) not

Page 84: 8 - OOP - Smalltalk Syntax

S.Ducasse 84

Summary

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

Page 85: 8 - OOP - Smalltalk Syntax

S.Ducasse 85

RoadmapFun with unary messages

Page 86: 8 - OOP - Smalltalk Syntax

S.Ducasse 86

1 class

Page 87: 8 - OOP - Smalltalk Syntax

S.Ducasse 87

1 class> SmallInteger

Page 88: 8 - OOP - Smalltalk Syntax

S.Ducasse 88

false not

Page 89: 8 - OOP - Smalltalk Syntax

S.Ducasse 89

false not> true

Page 90: 8 - OOP - Smalltalk Syntax

S.Ducasse 90

Date today

Page 91: 8 - OOP - Smalltalk Syntax

S.Ducasse 91

Date today> 24 May 2009

Page 92: 8 - OOP - Smalltalk Syntax

S.Ducasse 92

Time now

Page 93: 8 - OOP - Smalltalk Syntax

S.Ducasse 93

Time now> 6:50:13 pm

Page 94: 8 - OOP - Smalltalk Syntax

S.Ducasse 94

Float pi

Page 95: 8 - OOP - Smalltalk Syntax

S.Ducasse 95

Float pi> 3.141592653589793

Page 96: 8 - OOP - Smalltalk Syntax

S.Ducasse 96

We sent messages to objects or classes!

1 classDate today

Page 97: 8 - OOP - Smalltalk Syntax

S.Ducasse 97

We sent messages to objects or classes!

1 classDate today

Page 98: 8 - OOP - Smalltalk Syntax

S.Ducasse 98

Fun with binary messages

Roadmap

Page 99: 8 - OOP - Smalltalk Syntax

S.Ducasse 99

aReceiver aSelector anArgument

Used for arithmetic, comparison and logical operationsOne or two characters taken from: + - / \ * ~ < > = @ % | & ! ? ,

Page 100: 8 - OOP - Smalltalk Syntax

S.Ducasse 100

1 + 2

Page 101: 8 - OOP - Smalltalk Syntax

S.Ducasse 101

1 + 2>3

Page 102: 8 - OOP - Smalltalk Syntax

S.Ducasse 102

2 => 3

Page 103: 8 - OOP - Smalltalk Syntax

S.Ducasse 103

2 => 3> false

Page 104: 8 - OOP - Smalltalk Syntax

S.Ducasse 104

10 @ 200

Page 105: 8 - OOP - Smalltalk Syntax

S.Ducasse 105

‘Black chocolate’ , ‘ is good’

Page 106: 8 - OOP - Smalltalk Syntax

S.Ducasse 106

RoadmapFun with keyword-based messages

Page 107: 8 - OOP - Smalltalk Syntax

S.Ducasse 107

#(‘Calvin’ ‘hates’ ‘Suzie’) at: 2 put: ‘loves’

Page 108: 8 - OOP - Smalltalk Syntax

S.Ducasse 108

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

Page 109: 8 - OOP - Smalltalk Syntax

S.Ducasse 109

10@20 setX: 2

Page 110: 8 - OOP - Smalltalk Syntax

S.Ducasse 110

10@20 setX: 2> 2@20

Page 111: 8 - OOP - Smalltalk Syntax

S.Ducasse 111

12 between: 10 and: 20

Page 112: 8 - OOP - Smalltalk Syntax

S.Ducasse 112

12 between: 10 and: 20> true

Page 113: 8 - OOP - Smalltalk Syntax

S.Ducasse 113

receiver keyword1: argument1

keyword2: argument2

equivalent to receiver.keyword1keyword2(argument1, argument2)

Page 114: 8 - OOP - Smalltalk Syntax

S.Ducasse 114

receiver keyword1: argument1

keyword2: argument2

equivalent to receiver.keyword1keyword2(argument1, argument2)

Page 115: 8 - OOP - Smalltalk Syntax

S.Ducasse 115

Browser newOnClass: Point

Roadmap

Page 116: 8 - OOP - Smalltalk Syntax

S.Ducasse 116

Browser newOnClass: Point

Page 117: 8 - OOP - Smalltalk Syntax

S.Ducasse 117

Browser newOnClass: Point

Page 118: 8 - OOP - Smalltalk Syntax

S.Ducasse 118

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

Page 119: 8 - OOP - Smalltalk Syntax

S.Ducasse 119

Browser newOnClass: Point> a Browser

Page 120: 8 - OOP - Smalltalk Syntax

S.Ducasse 120

Doing and do not care of the returned result

Browser newOnClass: Point

Page 121: 8 - OOP - Smalltalk Syntax

S.Ducasse 121

Doing and really want to see the result!

10@20 setX: 2> 2@20

Page 122: 8 - OOP - Smalltalk Syntax

S.Ducasse 122

Doing vs printing (doing + print result)

Page 123: 8 - OOP - Smalltalk Syntax

S.Ducasse 123

Doing vs printing (doing + print result)

Page 124: 8 - OOP - Smalltalk Syntax

S.Ducasse 124

RoadmapMessages messagesmessagesagain messages....

Page 125: 8 - OOP - Smalltalk Syntax

S.Ducasse 125

Yes there are only messages unary binary keywords

Page 126: 8 - OOP - Smalltalk Syntax

S.Ducasse 126

Composition: from left to right!

69 class inspect

69 class superclass superclass inspect

Page 127: 8 - OOP - Smalltalk Syntax

S.Ducasse 127

Unary> Binary> Keywords

Precedence

Page 128: 8 - OOP - Smalltalk Syntax

S.Ducasse 128

2 + 3 squared

Page 129: 8 - OOP - Smalltalk Syntax

S.Ducasse 129

2 + 3 squared> 2 + 9

Page 130: 8 - OOP - Smalltalk Syntax

S.Ducasse 130

2 + 3 squared> 2 + 9> 11

Page 131: 8 - OOP - Smalltalk Syntax

S.Ducasse 131

Color gray - Color white = Color black

Page 132: 8 - OOP - Smalltalk Syntax

S.Ducasse 132

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

Page 133: 8 - OOP - Smalltalk Syntax

S.Ducasse 133

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

Page 134: 8 - OOP - Smalltalk Syntax

S.Ducasse 134

2 raisedTo: 3 + 2

Page 135: 8 - OOP - Smalltalk Syntax

S.Ducasse 135

2 raisedTo: 3 + 2> 2 raisedTo: 5

Page 136: 8 - OOP - Smalltalk Syntax

S.Ducasse 136

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

Page 137: 8 - OOP - Smalltalk Syntax

S.Ducasse 137

1/3 + 2/3

No mathematical precedence

Page 138: 8 - OOP - Smalltalk Syntax

S.Ducasse 138

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

No mathematical precedence

Page 139: 8 - OOP - Smalltalk Syntax

S.Ducasse 139

Parenthesized takes precedence!

(Msg) > Unary> Binary> Keywords

Page 140: 8 - OOP - Smalltalk Syntax

S.Ducasse 140

(0@0 extent: 100@100) bottomRight

Page 141: 8 - OOP - Smalltalk Syntax

S.Ducasse 141

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

Page 142: 8 - OOP - Smalltalk Syntax

S.Ducasse 142

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

Page 143: 8 - OOP - Smalltalk Syntax

S.Ducasse 143

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

Page 144: 8 - OOP - Smalltalk Syntax

S.Ducasse 144

0@0 extent: 100@100 bottomRight

Page 145: 8 - OOP - Smalltalk Syntax

S.Ducasse 145

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

Page 146: 8 - OOP - Smalltalk Syntax

S.Ducasse 146

3 + 2 * 10

No mathematical precedence

Page 147: 8 - OOP - Smalltalk Syntax

S.Ducasse 147

3 + 2 * 10> 5 * 10

No mathematical precedence

Page 148: 8 - OOP - Smalltalk Syntax

S.Ducasse 148

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

No mathematical precedence

Page 149: 8 - OOP - Smalltalk Syntax

S.Ducasse 149

3 + (2 * 10)

No mathematical precedence

Page 150: 8 - OOP - Smalltalk Syntax

S.Ducasse 150

3 + (2 * 10)> 3 + 20

No mathematical precedence

Page 151: 8 - OOP - Smalltalk Syntax

S.Ducasse 151

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

No mathematical precedence

Page 152: 8 - OOP - Smalltalk Syntax

S.Ducasse 152

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

No mathematical precedence

Page 153: 8 - OOP - Smalltalk Syntax

S.Ducasse 153

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

No mathematical precedence

Page 154: 8 - OOP - Smalltalk Syntax

S.Ducasse 154

(1/3) + (2/3)

No mathematical precedence

Page 155: 8 - OOP - Smalltalk Syntax

S.Ducasse 155

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

No mathematical precedence

Page 156: 8 - OOP - Smalltalk Syntax

S.Ducasse 156

(Msg) > Unary > Binary > Keywordsfrom left to rightNo mathematical precedence

Only Messages

Page 157: 8 - OOP - Smalltalk Syntax

S.Ducasse 157

(Msg) > Unary > Binary > Keywordsfrom left to rightNo mathematical precedence

Only Messages

Page 158: 8 - OOP - Smalltalk Syntax

S.Ducasse 158

RoadmapFun with blocks

Page 159: 8 - OOP - Smalltalk Syntax

S.Ducasse 159

Function definition

fct(x) = x * x + x

Page 160: 8 - OOP - Smalltalk Syntax

S.Ducasse 160

Function Application

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

Page 161: 8 - OOP - Smalltalk Syntax

S.Ducasse 161

Function definition

fct(x) = x * x + x

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

Page 162: 8 - OOP - Smalltalk Syntax

S.Ducasse 162

Function Application

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

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

Page 163: 8 - OOP - Smalltalk Syntax

S.Ducasse 163

Other examples

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

Page 164: 8 - OOP - Smalltalk Syntax

S.Ducasse 164

Block

anonymous method

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

value: ...

Page 165: 8 - OOP - Smalltalk Syntax

S.Ducasse 165

Block

anonymous methodReally really cool!Can be passed to methods, stored in instance variables

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

value: ...

Page 166: 8 - OOP - Smalltalk Syntax

S.Ducasse 166

RoadmapFun with conditional

Page 167: 8 - OOP - Smalltalk Syntax

S.Ducasse 167

Example

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

Page 168: 8 - OOP - Smalltalk Syntax

S.Ducasse 168

Example

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

> ‘positive’

Page 169: 8 - OOP - Smalltalk Syntax

S.Ducasse 169

Yes ifTrue:ifFalse: is a message!

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

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

Page 170: 8 - OOP - Smalltalk Syntax

S.Ducasse 170

Booleans

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

Page 171: 8 - OOP - Smalltalk Syntax

S.Ducasse 171

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

But optimized by the compiler :)

Page 172: 8 - OOP - Smalltalk Syntax

S.Ducasse 172

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

Page 173: 8 - OOP - Smalltalk Syntax

S.Ducasse 173

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

Page 174: 8 - OOP - Smalltalk Syntax

S.Ducasse 174

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

Page 175: 8 - OOP - Smalltalk Syntax

S.Ducasse 175

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

Page 176: 8 - OOP - Smalltalk Syntax

S.Ducasse 176

Confused with () and [] ?

Page 177: 8 - OOP - Smalltalk Syntax

S.Ducasse 177

Only put [ ] when you do not the number of times something may be executed

(x isBlue) ifTrue: [ x schroumph ]

10 timesRepeat: [ self shout ]

Page 178: 8 - OOP - Smalltalk Syntax

S.Ducasse 178

Conditions are messages sent to boolean

(x isBlue) ifTrue: [ ]

Page 179: 8 - OOP - Smalltalk Syntax

S.Ducasse 179

Fun with loops

Roadmap

Page 180: 8 - OOP - Smalltalk Syntax

S.Ducasse 180

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

Page 181: 8 - OOP - Smalltalk Syntax

S.Ducasse 181

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

Page 182: 8 - OOP - Smalltalk Syntax

S.Ducasse 182

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

Page 183: 8 - OOP - Smalltalk Syntax

S.Ducasse 183

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

Page 184: 8 - OOP - Smalltalk Syntax

S.Ducasse 184

So yes there are real loops in Smalltalk!

to:do: to:by:do:are just messages send to integers

Page 185: 8 - OOP - Smalltalk Syntax

S.Ducasse 185

So yes there are real loops in Smalltalk!

to:do: to:by:do:are just messages send to integers

Page 186: 8 - OOP - Smalltalk Syntax

S.Ducasse 186

RoadmapFun with iterators

Page 187: 8 - OOP - Smalltalk Syntax

S.Ducasse 187

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

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

Page 188: 8 - OOP - Smalltalk Syntax

S.Ducasse 188

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

Page 189: 8 - OOP - Smalltalk Syntax

S.Ducasse 189

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

Page 190: 8 - OOP - Smalltalk Syntax

S.Ducasse 190

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

Page 191: 8 - OOP - Smalltalk Syntax

S.Ducasse 191

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

Page 192: 8 - OOP - Smalltalk Syntax

S.Ducasse 192

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

We can also do it that 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

Page 193: 8 - OOP - Smalltalk Syntax

S.Ducasse 193

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

Page 194: 8 - OOP - Smalltalk Syntax

S.Ducasse 194

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

Page 195: 8 - OOP - Smalltalk Syntax

S.Ducasse 195

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

Page 196: 8 - OOP - Smalltalk Syntax

S.Ducasse 196

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

Page 197: 8 - OOP - Smalltalk Syntax

S.Ducasse 197

How do: is implemented?

Page 198: 8 - OOP - Smalltalk Syntax

S.Ducasse 198

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)]

Page 199: 8 - OOP - Smalltalk Syntax

S.Ducasse 199

Some others... 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]

Page 200: 8 - OOP - Smalltalk Syntax

S.Ducasse 200

Some others... friends

#(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]

Page 201: 8 - OOP - Smalltalk Syntax

S.Ducasse 201

Some others... friends

#(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]

Page 202: 8 - OOP - Smalltalk Syntax

S.Ducasse 202

Some others... friends

#(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]

Page 203: 8 - OOP - Smalltalk Syntax

S.Ducasse 203

Some others... friends

#(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

Page 204: 8 - OOP - Smalltalk Syntax

S.Ducasse 204

Iterators are your best friends compact nice abstraction Just messages sent to collections

Page 205: 8 - OOP - Smalltalk Syntax

S.Ducasse 205

Iterators are your best friends compact nice abstraction Just messages sent to collections

Page 206: 8 - OOP - Smalltalk Syntax

S.Ducasse 206

A simple exercise

How do you define the method that does that?

#() -> ‘’ #(a) ->’a’ #(a b c) -> ‘a, b, c’

Page 207: 8 - OOP - Smalltalk Syntax

S.Ducasse 207

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

Page 208: 8 - OOP - Smalltalk Syntax

S.Ducasse 208

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

Page 209: 8 - OOP - Smalltalk Syntax

S.Ducasse 209

Page 210: 8 - OOP - Smalltalk Syntax

S.Ducasse 210

Messages Sequencemessage1 .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

Page 211: 8 - OOP - Smalltalk Syntax

S.Ducasse 211

Multiple messages to an objects ;

To send multiple messages to the same object

Transcript show: 1 printString.

Transcript cr

is equivalent to:

Transcript show: 1 printString ; cr

Page 212: 8 - OOP - Smalltalk Syntax

S.Ducasse 212

Hints ...

x isNil ifTrue: [...]

x includes: 3 ifTrue: [...]

is read as the message includes:ifTrue:

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

Page 213: 8 - OOP - Smalltalk Syntax

S.Ducasse 213

Page 214: 8 - OOP - Smalltalk Syntax

S.Ducasse 214

Smalltalk is funPure simple powerful

www.seaside.st(www.dabbledb.com)www.pharo-project.org