Mercurial > repos > other > SevenLanguagesInSevenWeeks
view 2-Io/day1-singletons.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 | 3d41d9d72cc9 |
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