Skip to content

Instantly share code, notes, and snippets.

@g-leech
Last active July 15, 2018 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save g-leech/4880b886af0ff6448e9b53e78918bb81 to your computer and use it in GitHub Desktop.
Save g-leech/4880b886af0ff6448e9b53e78918bb81 to your computer and use it in GitHub Desktop.
# Every thing is an object.
# Every interaction with an object is a message.
# You don’t instantiate classes; you clone other objects called prototypes.
# Objects remember their prototypes.
# Objects have slots. Slots contain objects, including method objects.
# A message returns the value in a slot or invokes the method in a slot.
# If an object can’t respond to a message, it sends that message to its prototype.
Vehicle := Object clone
Vehicle description := "Something to take you places"
Vehicle description = "Something"
Vehicle cylinders := 12
Vehicle slotNames
Vehicle output := method(list(description, cylinders) join("\n"))
Vehicle output
Vehicle getSlot("output")
Vehicle proto type
# create a new object called Car by sending the clone message to the Vehicle prototype
Car := Vehicle clone
# instances are lowercase
ferrari := Car clone
Car drive := method("Vroom" println)
# Prototype change changes all instances!!!
ferrari drive
# Objects are just containers of slots.
# Get a slot by sending its name to an object. If the slot isn’t there, Io calls the parent
# Types are objects
# Both bruce and Person are objects
Car drive := method("Vroom" println)
# global namespace
Lobby
# or
Contact := Object clone do(
(name ::= 1)
(address ::= nil)
(city ::= nil)
( fullAddress := method(list(name, address, city) join("\n")) )
)
holmes := Contact clone setName("Holmes") setAddress("221B Baker St") setCity("London")
BusinessContact := Contact clone do(
companyName ::= ""
fullAddress := method( list(companyName, "Care of: " .. name, address, city) join("\n") )
)
steve := BusinessContact clone do(
setName("Steve")
setCompanyName("Apple Inc.")
setAddress("1 Infinite Loop")
setCity("Cupertino")
)
a := 2
if (a == 1, "a is one", "a is not one")
d := List clone append(30, 10, 5, 20)
d append("Find")
d size
d at(2)
d atPut(1, 123)
d select(>10)
d map(*2)
for(i, 1, 10, write(i, " "))
d foreach(i, v, writeln(i, ": ", v))
dict := Map clone
dict atPut("hello", "a greeting") dict
dict hasKey("hello") true
dict hasValue("a greeting") true
dict at("hello")
a := "foo"
b := "bar"
c := a .. b
c at(0) asCharacter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment