Top Banner
Understanding Messages Damien Cassou, Stéphane Ducasse and Luc Fabresse W2S01 http://www.pharo.org
20

Understanding Messagesrmod-pharo-mooc.lille.inria.fr/.../C019-W2S01-Messages.pdfUnderstanding Messages Author Damien Cassou, Stéphane Ducasse and Luc Fabresse Created Date 5/12/2020

Mar 06, 2021

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Understanding Messagesrmod-pharo-mooc.lille.inria.fr/.../C019-W2S01-Messages.pdfUnderstanding Messages Author Damien Cassou, Stéphane Ducasse and Luc Fabresse Created Date 5/12/2020

UnderstandingMessages

Damien Cassou, Stéphane Ducasse and Luc Fabresse

W2S01

http://www.pharo.org

Page 2: Understanding Messagesrmod-pharo-mooc.lille.inria.fr/.../C019-W2S01-Messages.pdfUnderstanding Messages Author Damien Cassou, Stéphane Ducasse and Luc Fabresse Created Date 5/12/2020

Objects, Messages and Closures

We only manipulate objects (mouse, booleans, arrays,numbers, strings, ...)

We only send them messages (@, +, not, getPng:,ifTrue:ifFalse:, new, ...)

and we use closures

W2S01 2 / 20

Page 3: Understanding Messagesrmod-pharo-mooc.lille.inria.fr/.../C019-W2S01-Messages.pdfUnderstanding Messages Author Damien Cassou, Stéphane Ducasse and Luc Fabresse Created Date 5/12/2020

Syntax

Originally invented for kids Programs look like little sentences Try to minimize the number of parentheses

W2S01 3 / 20

Page 4: Understanding Messagesrmod-pharo-mooc.lille.inria.fr/.../C019-W2S01-Messages.pdfUnderstanding Messages Author Damien Cassou, Stéphane Ducasse and Luc Fabresse Created Date 5/12/2020

Example

(ZnEasy getPng: 'http://a.tile.openstreetmap.org/8/12/8.png')asMorph openInWindow

W2S01 4 / 20

Page 5: Understanding Messagesrmod-pharo-mooc.lille.inria.fr/.../C019-W2S01-Messages.pdfUnderstanding Messages Author Damien Cassou, Stéphane Ducasse and Luc Fabresse Created Date 5/12/2020

Three Kinds of Messages to Minimize Parentheses

Unary message: receiver selector◦ 9 squared, Date today

Binary message: receiver selector argument◦ 1+2◦ 3@4

Keyword message: receiver key1: arg1 key2: arg2◦ 2 between: 10 and: 20

W2S01 5 / 20

Page 6: Understanding Messagesrmod-pharo-mooc.lille.inria.fr/.../C019-W2S01-Messages.pdfUnderstanding Messages Author Damien Cassou, Stéphane Ducasse and Luc Fabresse Created Date 5/12/2020

Message Precedence

(Msg) > Unary > Binary > Keywords First we execute () Then unary, then binary and finally keyword messages

This order minimizes () needsBut let us start with messages

W2S01 6 / 20

Page 7: Understanding Messagesrmod-pharo-mooc.lille.inria.fr/.../C019-W2S01-Messages.pdfUnderstanding Messages Author Damien Cassou, Stéphane Ducasse and Luc Fabresse Created Date 5/12/2020

Unary Message Examples

anObject aSelector

1 class> SmallInteger

false not> true

Date today> 24 May 2009

Float pi> 3.141592653589793

W2S01 7 / 20

Page 8: Understanding Messagesrmod-pharo-mooc.lille.inria.fr/.../C019-W2S01-Messages.pdfUnderstanding Messages Author Damien Cassou, Stéphane Ducasse and Luc Fabresse Created Date 5/12/2020

Did you notice?

We sent messages to any objects, including classes! There is no difference between sending a message to an

object or to a class

1 class> SmallInteger

Date today> 27 June 2015

W2S01 8 / 20

Page 9: Understanding Messagesrmod-pharo-mooc.lille.inria.fr/.../C019-W2S01-Messages.pdfUnderstanding Messages Author Damien Cassou, Stéphane Ducasse and Luc Fabresse Created Date 5/12/2020

A Bit of Introspection

Point selectors> #(#x #theta #quadrantOf: #onLineFrom:to:within:

#bitShi�Point: #< #scaleFrom:to: #sideOf: #'\\' #scaleTo:#grid: #'//' #asIntegerPoint #directionToLineFrom:to: ...)

Returns all the messages the class understands

W2S01 9 / 20

Page 10: Understanding Messagesrmod-pharo-mooc.lille.inria.fr/.../C019-W2S01-Messages.pdfUnderstanding Messages Author Damien Cassou, Stéphane Ducasse and Luc Fabresse Created Date 5/12/2020

A Little Query Let us query the system and only filter the unary

messages:

Point selectors select: #isUnary> #(#x #theta #asIntegerPoint #r #negated #normalized #sign#degrees #isIntegerPoint #guarded #fourNeighbors#eightNeighbors #min #max #ceiling #normal #asPoint #y#abs #isPoint #angle #transposed #reciprocal#asFloatPoint #asNonFractionalPoint #rounded#le�Rotated #floor #truncated #hash #deepCopy#fourDirections #rightRotated #isSelfEvaluating #asMargin#isZero)

select: is an iterator (see Iterator lecture) Easy :-)

W2S01 10 / 20

Page 11: Understanding Messagesrmod-pharo-mooc.lille.inria.fr/.../C019-W2S01-Messages.pdfUnderstanding Messages Author Damien Cassou, Stéphane Ducasse and Luc Fabresse Created Date 5/12/2020

Binary Messages

anObject aBinarySelector anArgument

Used for arithmetic, comparison and logical operations One, two or three characters taken from:

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

W2S01 11 / 20

Page 12: Understanding Messagesrmod-pharo-mooc.lille.inria.fr/.../C019-W2S01-Messages.pdfUnderstanding Messages Author Damien Cassou, Stéphane Ducasse and Luc Fabresse Created Date 5/12/2020

Binary Message Examples

1 + 2> 3

2 > 3> false

10@200> 10@200

'Black chocolate' , ' is good'> 'Black chocolate is good'

W2S01 12 / 20

Page 13: Understanding Messagesrmod-pharo-mooc.lille.inria.fr/.../C019-W2S01-Messages.pdfUnderstanding Messages Author Damien Cassou, Stéphane Ducasse and Luc Fabresse Created Date 5/12/2020

Keyword Messages

anObject keyword1: arg1 keyword2: arg2

equivalent to:

receiver.keyword1keyword2(arg1, arg2)

W2S01 13 / 20

Page 14: Understanding Messagesrmod-pharo-mooc.lille.inria.fr/.../C019-W2S01-Messages.pdfUnderstanding Messages Author Damien Cassou, Stéphane Ducasse and Luc Fabresse Created Date 5/12/2020

Test Yourself!

1 log Browser open 2 raisedTo: 5 ’hello’, ’world’ 10@20 point1 x point1 distanceFrom: point2

W2S01 14 / 20

Page 15: Understanding Messagesrmod-pharo-mooc.lille.inria.fr/.../C019-W2S01-Messages.pdfUnderstanding Messages Author Damien Cassou, Stéphane Ducasse and Luc Fabresse Created Date 5/12/2020

Test Yourself!

1 log (unary) Browser open (unary) 2 raisedTo: 5 (keyword) ’hello’, ’world’ (binary) 10@20 (binary) point1 x (unary) point1 distanceFrom: point2 (keyword)

W2S01 15 / 20

Page 16: Understanding Messagesrmod-pharo-mooc.lille.inria.fr/.../C019-W2S01-Messages.pdfUnderstanding Messages Author Damien Cassou, Stéphane Ducasse and Luc Fabresse Created Date 5/12/2020

Example: Message setX:

10@20 setX: 2> 2@20

We change the x value of the receiver (a point) No parentheses required

W2S01 16 / 20

Page 17: Understanding Messagesrmod-pharo-mooc.lille.inria.fr/.../C019-W2S01-Messages.pdfUnderstanding Messages Author Damien Cassou, Stéphane Ducasse and Luc Fabresse Created Date 5/12/2020

Example: Message at:put:

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

#(...) creates an array at:put: changes the value of the array element. arrays start at 1 in Pharo (i.e., first element is at index 1)

W2S01 17 / 20

Page 18: Understanding Messagesrmod-pharo-mooc.lille.inria.fr/.../C019-W2S01-Messages.pdfUnderstanding Messages Author Damien Cassou, Stéphane Ducasse and Luc Fabresse Created Date 5/12/2020

Example: Message between:and:

12 between: 10 and: 20> true

The message between:and: is sent to an integer Takes two arguments 10 and 20

W2S01 18 / 20

Page 19: Understanding Messagesrmod-pharo-mooc.lille.inria.fr/.../C019-W2S01-Messages.pdfUnderstanding Messages Author Damien Cassou, Stéphane Ducasse and Luc Fabresse Created Date 5/12/2020

Summary

Three kinds of messages: unary, binary and keywords Unary

◦ 5 factorial Binary

◦ 2 + 3 Keywords-based messages

◦ 2 between: 0 and: 10

W2S01 19 / 20

Page 20: Understanding Messagesrmod-pharo-mooc.lille.inria.fr/.../C019-W2S01-Messages.pdfUnderstanding Messages Author Damien Cassou, Stéphane Ducasse and Luc Fabresse Created Date 5/12/2020

A course by

and

in collaboration with

Inria 2020

Except where otherwise noted, this work is licensed under CC BY-NC-ND 3.0 Francehttps://creativecommons.org/licenses/by-nc-nd/3.0/fr/