Mercurial > repos > other > SevenLanguagesInSevenWeeks
changeset 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 | 4e1a659f8383 |
children | 22edfbf3b8bd |
files | 2-Io/day1-singletons.io |
diffstat | 1 files changed, 36 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /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