next up previous contents index
Next: Classes Up: An Example Previous: An Example   Contents   Index


Instance Variables

The dream snooze is created with local instantiations of the variables X and Y. A copy of this dream is made and called nap. A thought that prints the variables X, Y, and Z, is defined and given the name hmm.

The thought hmm is pondered in reality to demonstrate its effect. It prints the values of X, Y, and Z, as they exist in the Forth system. This same thought is then pondered in the dream snooze, which produces the same effect: the same values of X, Y, and Z are displayed.

Now the values of X, Y, and Z are altered in the understanding of reality. When hmm is again pondered in reality, these new values are displayed, but when it is pondered in snooze, the old values of X and Y are displayed together with the new value of Z.

What has happened here? When snooze was created, it contained local versions of X and Y. The values of these variables were inherited from the values that they had when the copy was made, but snooze now has its own private versions of these variables. When hmm was first pondered in snooze, the original values were displayed. When the values of these variables were later changed, this change had no effect on the private copies of X and Y in snooze. Therefore, when hmm was again pondered in snooze, the old private values were still displayed. The new value of Z was displayed, however, because Z was not local to snooze, and so the same Z was known both to snooze and to reality.

An unnamed thought is then pondered in snooze to modify the values of the three variables within the understanding of that dream. When these variables are displayed in the understanding of snooze, the latest values are output, but when they are displayed in the understanding of reality, only the value of Z shows the change made in snooze. When the variables are displayed in the understanding of the dream nap, the values that were active when nap was copied are still active. All of these exercises serve to demonstrate the way dreams provide support for a form of local variables.


next up previous contents index
Next: Classes Up: An Example Previous: An Example   Contents   Index
Robert J. Brown
1999-09-26