Combining Proto and Parent Inheritance

Using self in a Method

Here are rules of thumb for using self within a method:

Rationale: Although the meanings of the two are identical, it is confusing for the reader. self:Message() looks very similar to self.slot. They operate differently because self:Message() follows parent inheritance, but self.slot does not. Things that look similar but work differently invariably cause confusion.

Rationale: self.slot doesn't use parent inheritance, so NewtonScript won't ever find it if it is up there.

Rationale: If you don't use self., and the slot does not exist in the proto chain, you will end up with a local variable being created instead of a slot. The explicit self.slot means that slot will be created in self whether or not it exists in the proto chain.

Rationale: If you use code like slot := value (instead of self.slot := value), slot will be found in the proto chain, and slot will be created in self. If you don't create slot at compile time, slot won't be found in the proto chain and will be created as a local variable instead.

Although this rule of thumb isn't necessary if you follow the previous rule of thumb (using self.slot := value), the two rules are an example of the belt- and-suspenders approach to life. Even if you forget one, the other will keep you from harm.


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

Last modified: 1 DEC 1996