Message Sending

Effects of Variable Lookup Rules

These lookup rules have a number of repercussions.

You can now rewrite the bankBalance frame and its three methods to take advantage of these rules:

bankBalance := {
   total: 0,
   Deposit:    func(amount) 
               begin
                  total := total + amount;
               end,
   Withdraw:   func(amount)
               begin
                  total := total - amount;
               end,
   Balance:    func() 
                  return total,
};

Caution:See "Using self in a Method" on page 100 for a complete description of when to use self and when not to use it.


Another effect of the lookup rules involves variable declaration:

Thus, you could rewrite the Max method without ever declaring maxSoFar:

aFrame := {
   Max3 :   func(a, b, c)
            begin
               maxSoFar := a;

               if b > maxSoFar then maxSoFar := b;
               if c > maxSoFar then maxSoFar := c;
               return maxSoFar;
            end,
}
The first time maxSoFar is assigned, the assignment lookup rules come into play. First, maxSoFar is looked for as a local, then as a slot in aFrame, then as a global. When these three rules fail to produce maxSoFar, it is created as a local in Max3.

Nevertheless, you should still declare your local variables. There are a number of compelling reasons for this:


An online version of Programming for the Newton using Macintosh, 2nd ed. ©1996, 1994, Julie McKeehan and Neil Rhodes.

Last modified: 1 DEC 1996