view 2-Io/day2-operators.io @ 56:2bbb377ddeb0

Add more examples, more thoughts and a three-argument "collaborated" rule
author IBBoard <dev@ibboard.co.uk>
date Tue, 26 Sep 2017 20:45:26 +0100
parents 86668d32e162
children
line wrap: on
line source

# Operators are syntactic sugar and can be listed from a table:
OperatorTable println

# Creating an operator involves defining it with a precedence
# (index based on the output from the previous table)
OperatorTable addOperator("xor", 11)

# And then implementing it on appropriate prototypes
# Note that Io is open-edit, like Ruby, so we can screw with core stuff
# like Booleans and extend them
true xor := method(bool, if(bool, false, true))
false xor := method(bool, if(bool, true, false))

# For some reason I have to work out, the prints don't work here
# They print the second value. But running in REPL works and return correct
# value (before printing wrong one)
(true xor true) println
(true xor false) println
(false xor true) println
(false xor false) println