# HG changeset patch # User IBBoard # Date 1504722560 -3600 # Node ID 3d41d9d72cc916463e1fcb85e30d82491f3d2775 # Parent 4e1a659f83830935e2830235b282a3b419f3eb6e Add Day 1 code for Singletons (including booleans) diff -r 4e1a659f8383 -r 3d41d9d72cc9 2-Io/day1-singletons.io --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/2-Io/day1-singletons.io Wed Sep 06 19:29:20 2017 +0100 @@ -0,0 +1,36 @@ +# 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 \ No newline at end of file