view 2-Io/day2-messages.io @ 67:8906b5a4517f

Calculate and validate boxes The solver now creates answers for valid sudoku, but doesn't reject invalid ones (e.g. dupe in column)
author IBBoard <dev@ibboard.co.uk>
date Sat, 07 Oct 2017 15:07:34 +0100
parents 86668d32e162
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"))