Search

September 16, 2012

Well I am back to reading Seven Languages in Seven Days by Bruce Tate and am taking on the chapter on Io. If you are not familiar, Io is a prototype-based language like JavaScript. Since I typically work on the server-side and only dabble in JavaScript and HTML, I am looking forward to seeing how learning Io can reflect on my knowledge of JavaScript.

The first thing to grab my attention is how slots on clones are handled. You’ll notice that the Car created from the Vehicle clone does not have a description slot listed when the slotNames message is sent to it. Also, Tate indicates that when you send the description message to Car, the message is forwarded to the prototype, Vehicle. Let’s see how that shakes out:

Io 20110905
Io> Vehicle := Object clone
==>  Vehicle_0x9be5758:
  type             = "Vehicle"

Io> Vehicle description := "Something to take you far away"
==> Something to take you far away
Io> Vehicle slotNames
==> list(description, type)
Io> Car := Vehicle clone
==>  Car_0x9c35590:
  type             = "Car"

Io> Car slotNames
==> list(type)
Io> Car description
==> Something to take you far away
Io> Vehicle description = "Something that can move you"
==> Something that can move you
Io> Car description
==> Something that can move you

Interesting, changing the description slot on Vehicle is reflected when the description message is sent to Car. But apparently it can be overridden:

Io> Car description = "Something else entirely"
==> Something else entirely
Io> Car description
==> Something else entirely
Io> Vehicle description
==> Something that can move you
Io>

Interestingly you can use the weaker = assignment even though in one sense the description slot had not been defined on Car.

Here’s another question: can we clone non-types and what is the behavior? In turns out the behavior is pretty much the same, except that the prototype is listed as the prototype of the cloned object:

Io> anotherFerrari := ferrari clone
==>  Car_0x9b61418:

Io> ferrari slotNames
==> list()
Io> ferrari color := "red"
==> red
Io> ferrari color
==> red
Io> anotherFerrari color
==> red
Io> Car color

  Exception: Car does not respond to 'color'
  ---------
  Car color                            Command Line 1

Io> anotherFerrari proto
==>  Car_0x9cb92d0:
  color            = "red"

Moving on to the exercises, most are straightforward. However, following my nose lead me to an interesting place when trying to execute the code in a slot given its name.

Io 20110905
Io> x := Object clone
==>  Object_0x9f878b0:

Io> x yzzy := method("plugh" println; return self)
==> method(
    "plugh" println; return self
)
Io> x yzzy
plugh
==>  Object_0x9f878b0:
  yzzy             = method(...)

Io> x getSlot("yzzy")
==> method(
    "plugh" println; return self
)
Io> x getSlot("yzzy") type
==> Block
Io> x getSlot("yzzy") call
plugh
==>  Object_0x9f13028:
  Lobby            = Object_0x9f13028
  Protos           = Object_0x9f12f58
  _                = Object_0x9f13028
  exit             = method(...)
  forward          = method(...)
  set_             = method(...)
  x                = Object_0x9f878b0

Io> x perform("yzzy")
plugh
==>  Object_0x9f878b0:
  yzzy             = method(...)

Initially I tried to get to the code via getSlot. While this worked, I ended up with a Block and then tried sending the call message to it. The code was executed, but the right thing was not returned. Somehow I ended up with the Lobby being returned instead of x. It turned out the better approach was to use the perform method on Object. Now the correct value is returned.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.