annotate 2-Io/day2-messages.io @ 71:32f018861e36

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