The init method and the prototype chain

View: New views
2 Messages — Rating Filter:   Alert me  

The init method and the prototype chain

by Chris Double :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

It seems that the 'protos' slot is empty when the 'init' method of an
object is called during a clone operation. The 'protos' slot is then
set after init is called. I wanted to add to the prototype chain
during init - is this possible at all? If not, what's an approach for
doing this?

An example:

---------------8<----------------
$ io
Io 20090105
Io> Foo := Object clone
==>  Foo_0x822ca28:
  type             = "Foo"

Io> Bar := Object clone
==>  Bar_0x8283830:
  type             = "Bar"

Io> Baz := Foo clone do (init := method(appendProto(Bar clone)))
==>  Baz_0x81792c0:
  init             = method(...)
  type             = "Baz"

Io> Baz clone protos
==> list(Baz_0x81792c0)
---------------8<----------------

Chris
--
http://www.bluishcoder.co.nz

Re: The init method and the prototype chain

by Chris Double :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wed, Jul 15, 2009 at 4:21 PM, Chris Double<chris.double@...> wrote:
> Io> Baz := Foo clone do (init := method(appendProto(Bar clone)))

It was pointed out to me on IRC to change this line to:

> Baz := Foo clone do (init := method(self appendProto(Bar clone)))

Using the implicit receiver results in appendProto being sent to the
frame object rather than the newly cloned Foo. Making the change to
using an explicit 'self' fixes the problem.

Chris.
--
http://www.bluishcoder.co.nz