view 2-Io/day2-messages.io @ 37:86668d32e162

Add Day 2 code
author IBBoard <dev@ibboard.co.uk>
date Tue, 12 Sep 2017 20:58:51 +0100
parents
children
line wrap: on
line source

# Everything is a message in Io, apparently. Everything.
# Messages have senders, receivers and args, and you can get and use those objects.
# So the example makes a post office.
postOffice := Object clone
# Add a method that returns the sender object
postOffice packageSender := method(call sender)
"Post office: " print
postOffice println
mailer := Object clone
# Call the PostOffice's method
mailer deliver := method(postOffice packageSender)
"Mailer: " print
mailer println
# Calling the method returns the caller (sender via the postOffice object)
mailer deliver println

# We can also get message arguments and name
# Let's do this a little differently to the book!
postOffice messageDetails := method(call message name println; call message arguments println)
postOffice messageDetails("One", 2, :three)

# Because of how args are passed unevaluated then we can make our own "unless" method
unless := method(
    (call sender doMessage(call message argAt(0))) \
        ifFalse(call sender doMessage(call message argAt(1))) \
        ifTrue(call sender doMessage(call message argAt(2)))
)
# Note the line continuations - this seems more readable than the approach in the book of
# leaving the "ifFalse(" and "ifTrue(" on the previous line!
# Also, this is an IF in the form "[boolean statement] ifFalse(code) ifTrue(code)", not the earlier
# "if(boolean, true-code, false-code)" form

unless (1 == 2, write("One is not two\n"), write("OMG! Maths has broken!!!!\n"))