Messages for Java Programmers

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

Only Objects and Messages

Equivalence

In Java

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

In Pharo

strings := OrderedCollection new.

Equivalence

In Java

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

In Pharo

[ self doSomething ] fork

Equivalence

In Java 8

new Thread(() -> this.doSomething()).start();

In Pharo

[ self doSomething ] fork

Three Kinds of Messages

5 factorial
Transcript cr
3 + 4
5 -> 10
Transcript show: 'hello world'
2 between: 0 and: 5

Keyword Messages for Java developers

In Java

    receiver.keyword1keyword2(arg1, arg2)

In Pharo

    anObject keyword1: arg1 keyword2: arg2

Keyword Messages for Java developers

In Java

    postman.send(mail,recipient);

Keyword Messages for Java developers

    postman.send(mail,recipient);
    postman . send ( mail , recipient );

Keyword Messages for Java developers

    postman.send(mail,recipient);
    postman . send ( mail , recipient );
    postman send mail recipient

Keyword Messages for Java developers

    postman.send(mail,recipient);
    postman . send ( mail , recipient );
    postman send mail recipient
    postman send mail to recipient

Keyword Messages for Java developers

    postman.send(mail,recipient);
    postman . send ( mail , recipient );
    postman send mail recipient
    postman send mail to recipient
    postman send: mail to: recipient

Keyword Messages for Java developers

In Java

    postman.send(mail,recipient);

In Pharo

    postman send: mail to: recipient

Conditionals are Just Messages

fullName isEmpty
   ifTrue: [ 'FirstnameLastname' ]
   ifFalse: [ fullName ]

Loops are Just Messages

4 timesRepeat: [ self doSomething ]
0 to: 100 do: [ :i | ... ]
0 to: 100 by: 3 do: [ :i | ... ]
aCollection do: [ :each | ... ]

Summary

/