view 2-Io/day1-singletons.io @ 93:39084e2b8744

Add a function for word-aware text wrapping Potentially hugely inefficient because we iterate through the string character by character, but then splitting it first and iterating over words still needs to iterate over the string to know where to split.
author IBBoard <dev@ibboard.co.uk>
date Tue, 18 Jun 2019 21:05:00 +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