Inheritance in NewtonScript
FIGURE 4.6 : Inheritance in a simple Newton Application.
FIGURE 4.7 : Inheritance chain and lookup order.
_parent
and _proto
slots in them. One of the frames has a method, SillyMethod
, that makes assignments to slots in all of the frames.
Let us walk through what happens when SillyMethod
is executed. The message we will send is:
one:SillyMethod();
one
is searched for SillyMethod
. When this fails, lookup follows the proto chain to two
and searches for SillyMethod
there. When lookup finds it, it sets self
to one
; SillyMethod
is executed; and its first assignment is made:
a := "apple";
a
is first looked up as a local in SillyMethod
. Then it is searched for in self
. When a
is found directly within one
, it gets assigned its new value right there.
The next statement in SillyMethod
:
b := "boat";
is executed and lookup begins for b
. First, it is looked up as a local in SillyMethod
. Using inheritance, self
(one
) is searched for b
, and then lookup follows the proto chain to two
. This frame contains a b
, so lookup is finished. To make a successful assignment requires creating a new slot with the value of "boat"
in one
(the same level at which the lookup found the slot). Remember, an assignment doesn't modify the proto.
SillyMethod
now executes the third assignment:
c := "cat";
Lookup begins for c
. SillyMethod
is searched first for a local c
. Using inheritance, self
(one
) is searched for c
, and then lookup follows the proto chain to two
. Now lookup, having exhausted the proto chain, starts up the parent chain of self
in the quest for c
. The parent of self
(one
) is three
. This frame contains a c
, so lookup is successful. The c
slot in three
is now assigned the new string value (as this is the level at which the lookup found the slot).
SillyMethod
now executes the fourth assignment:
d := "dog";
Lookup begins for a local d
in SillyMethod
. Using inheritance, self
is again searched for d
, and then lookup follows the proto chain to two
. Not finding the slot in the proto chain, lookup starts up the parent chain of self
in its quest for d
. three
does not contain d
, so lookup continues out the proto chain to four
. This frame contains a d
slot, so lookup is successful. Assignment to d
requires creating a new slot with the value of "dog"
in three
. Assignment only changes slots along the spine of the comb, and assignment occurs at the level at which a slot is found.
The lookup occurs in the same way for the last three assignments to e
, f
, and g
, though one more level up the parent chain. At this point, you will have new and modified slots on the comb's spine.
An online version of Programming for the Newton using Macintosh, 2nd ed. ©1996, 1994, Julie McKeehan and Neil Rhodes.
Last modified: 1 DEC 1996