Slot Assignment
copyCat
and original
and this assignment:
copyCat.sharedSlot4 := 69;creates a new slot in
copyCat
with the value of 69
(see FIGURE 4.5).
FIGURE 4.5 : Assignment to an inherited slot creates a new slot.
account
, and two others frames, savingsAccount
and checkingAccount
, that proto from it:
account:= { total: 0, Deposit: func(amount) begin total := total + amount; end, Withdraw:func(amount) begin total := total - amount; end, Balance: func() return total, };
savingsAccount := { _proto: account, };
checkingAccount := { _proto: account, };
We will send some messages to savingsAccount
and checkingAccount
and see what happens to the slots in our three frames:
savingsAccount:Deposit(2000); checkingAccount:Deposit(500); savingsAccount:Withdraw(200);Before sending the messages,
savingsAccount
and checkingAccount
only have one slot each, the _proto
slot.
When the Deposit
message is sent to savingsAccount
:
savingsAccount:Deposit(2000);the runtime system looks for the
Deposit
method within the savingsAccount
frame. Since it isn't there, it follows the proto pointer to account
. It finds the Deposit
method there and executes it.
While Deposit
is executing, the value of self
is savingsAccount
(self
is the frame that receives the message). When Deposit
attempts to access the current value of total
, lookup begins. total
is looked for first as a local variable, then using inheritance rules. Inheritance lookup starts with self
and then follows the proto chain. total
is found in account
, with a value of 0
.
When the Deposit
method:
tries to assign tototal := total + amount;
end;
total
, the lookup takes place again--starting first in self
and then following the proto chain. total
is found in account.
Notice that total
in account
is not changed; instead, a new total
slot with a new value is created in self
(savingsAccount
). This overrides the total
slot in account
.
When the Deposit
method finishes, savingsAccount
contains two slots, a _proto
and a total
slot. After you execute the next message to checking- Account
, the same process occurs all over again. When you have finished, checkingAccount
will also have two slots, a _proto
slot and a total
slot. When you execute the third message:
savingsAccount:Withdraw(200);
savingsAccount
will have no new slots, but the value of its total
slot will now be 1800
.
An online version of Programming for the Newton using Macintosh, 2nd ed. ©1996, 1994, Julie McKeehan and Neil Rhodes.
Last modified: 1 DEC 1996