view 2-Io/day1-singletons.io @ 34:3d41d9d72cc9

Add Day 1 code for Singletons (including booleans)
author IBBoard <dev@ibboard.co.uk>
date Wed, 06 Sep 2017 19:29:20 +0100
parents
children
line wrap: on
line source

# Conditions are simple and like most languages
# Not mentioned yet, but found by necessity: we can use brackets!
(4 < 5) println
# Unlike Python, Io uses lower case for booleans
(true and false) println
# Truthy values are more like Python than C:
(true and 6) println
(true and 0) println

# But true, false and nil are all just objects
# Albeit special "singleton" objects where cloning gets the same object
true clone println
false clone println
nil clone println
(true == true clone) println

# We can create our own singletons by altering the clone method to return the class
# Highlander: THERE CAN BE ONLY ONE!
Highlander := Object clone
Highlander clone := Highlander
Highlander println
Highlander clone println
highlander1 := Highlander clone
highlander2 := Highlander clone
(highlander1 == highlander2) println
# Normally cloning gives different objects with different IDs
obj1 := Object clone
obj2 := Object clone
(obj1 == obj2) println

# And with great power comes great responsibility.
# Object is not special. It is not protected in any way.
# DO NOT do this in real code!
Object clone := "Ooops!"
obj3 := Object clone
obj3 println