Friday, November 26, 2010

Io Language: the init method


I'm slowly working through Bruce Tate's book "7 Languages in 7 Weeks". It is ambitious and lots of fun. In the Io chapter I struggled and I think it was because Bruce forgot to mention Io's init method. The init method is automatically called anytime a prototype is cloned and it is important becuase if you can't setup each clone, you can't have full fledged instance level behavior. Here is some code I wrote after reading Bruce's intro to Io.

Closet := Object clone do(
closet := list()
stash := method(item,
self closet append(item)
)
peek := method(
self closet foreach(item, item println)
)
)

myCloset := Closet clone
yourCloset := Closet clone
myCloset stash("rollerskates")
yourCloset stash("bowling ball")
"My closet looks like:" println
myCloset peek

"Your closet looks like:" println
yourCloset peek

If you run that code you will see it print out the following:

My closet looks like:
rollerskates
bowling ball
Your closet looks like:
rollerskates
bowling ball

Whoops. My closets aren't behaving like separate instances! What is going on? Well, in prototype if an object receives a message and no slot exists to receive that message, it forwards that message up to its prototype. In this case, the closet slot is on the Closet prototype, so closet is like a class variable. It sounds simple, but it took me awhile to understand what was happening. I had been assuming that a "clone" was an object identical to its prototype, but it isn't identical at all. In fact, initially the closet clones have no state or behavior of their own. It felt like something was missing...

Of course! These clones need to be able to initialize themselves. With an init method each of the clones can have their own closet slot. No more shared state.

Closet := Object clone do(
init := method(
self closet := list()
)

stash := method(item,
self closet append(item)
)

peek := method(
self closet foreach(item, item println)
)
)

Run that the same way we did previously and you get:

My closet looks like:
rollerskates
Your closet looks like:
bowling ball

Which is what I meant originally.

They say learning 1 new language a year will make you a better programmer. Bruce's book can help keep you well ahead of that curve.

1 comment:

Anonymous said...

Good write up. One thing that commonly catches newcomers is the fact that the "clone" primitive is so named for historical reasons, rather than what it actually does. clone in fact, doesn't clone anything. It's entire algorithm is as follows: 1) Create a new empty object; 2) Set the protos list of the new object to contain one item: the receiver of the clone message. 3) Call init. That's it.