Understanding Messages

Damien Cassou, St├ęphane Ducasse and Luc Fabresse http://stephane.ducasse.free.fr

Objects, Messages and Closures

Syntax

Example

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

Three Kinds of Messages to Minimize Parentheses

Message Precedence

(Msg) > Unary > Binary > Keywords

This order minimizes () needs

But let us start with messages

Unary Message Examples

anObject aSelector
1 class
> SmallInteger
false not
> true
Date today
> 24 May 2009
Float pi
> 3.141592653589793

Did you notice?

1 class
> SmallInteger
Date today
> 27 June 2015

A Bit of Introspection

Point selectors
> #(#x #theta #quadrantOf: #onLineFrom:to:within: #bitShiftPoint: #< #scaleFrom:to: #sideOf: #'\\' #scaleTo: #grid: #'//' #asIntegerPoint #directionToLineFrom:to: ...) 

A Little Query

    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 #leftRotated #floor #truncated #hash #deepCopy #fourDirections #rightRotated #isSelfEvaluating #asMargin #isZero)

Binary Messages

anObject aBinarySelector anArgument

Binary Message Examples

1 + 2
> 3
2 > 3
> false
10@200 
> 10@200
'Black chocolate' , ' is good'
> 'Black chocolate is good'

Keyword Messages

anObject keyword1: arg1 keyword2: arg2

equivalent to:

receiver.keyword1keyword2(arg1, arg2)

Test Yourself!

Test Yourself!

Example: Message setX:

10@20 setX: 2
> 2@20

Example: Message at:put:

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

Example: Message between:and:

12 between: 10 and: 20
> true

Summary

Three kinds of messages: unary, binary and keywords

/