37
|
1 # Everything is a message in Io, apparently. Everything.
|
|
2 # Messages have senders, receivers and args, and you can get and use those objects.
|
|
3 # So the example makes a post office.
|
|
4 postOffice := Object clone
|
|
5 # Add a method that returns the sender object
|
|
6 postOffice packageSender := method(call sender)
|
|
7 "Post office: " print
|
|
8 postOffice println
|
|
9 mailer := Object clone
|
|
10 # Call the PostOffice's method
|
|
11 mailer deliver := method(postOffice packageSender)
|
|
12 "Mailer: " print
|
|
13 mailer println
|
|
14 # Calling the method returns the caller (sender via the postOffice object)
|
|
15 mailer deliver println
|
|
16
|
|
17 # We can also get message arguments and name
|
|
18 # Let's do this a little differently to the book!
|
|
19 postOffice messageDetails := method(call message name println; call message arguments println)
|
|
20 postOffice messageDetails("One", 2, :three)
|
|
21
|
|
22 # Because of how args are passed unevaluated then we can make our own "unless" method
|
|
23 unless := method(
|
|
24 (call sender doMessage(call message argAt(0))) \
|
|
25 ifFalse(call sender doMessage(call message argAt(1))) \
|
|
26 ifTrue(call sender doMessage(call message argAt(2)))
|
|
27 )
|
|
28 # Note the line continuations - this seems more readable than the approach in the book of
|
|
29 # leaving the "ifFalse(" and "ifTrue(" on the previous line!
|
|
30 # Also, this is an IF in the form "[boolean statement] ifFalse(code) ifTrue(code)", not the earlier
|
|
31 # "if(boolean, true-code, false-code)" form
|
|
32
|
|
33 unless (1 == 2, write("One is not two\n"), write("OMG! Maths has broken!!!!\n")) |