Previous | Table of Contents | Next |
Existence of the class as an object, however, opened the door to having any number of initialization messages sent to the class and then passed on to a newly created instance. The class could now have its own behaviorfor example, Date could respond to today with the current day and year. Beginning Smalltalk programmers are often confused by the notion of a class as both an object capable of responding to arbitrary messages and as a special kind of object creating instances of itself. The confusion is highlighted by the design of the typical Smalltalk browser, which makes no effort to display classes as ordinary objects yet simultaneously presents methods to the class itself as well as to its instances. The system is further confused because this capability of a metaclass to have instance-specific behavior (as defined in the class) is not shared by a class to have its own instance-specific behavior (as defined in the metaclass).
Similar treatment is given to the properties of objects. A class describes the property structure of its instances. These are called instance variables. A class as an object can have its own properties, and these are the classs instance variables. Instance variables are accessible only by methods defined in the context of the objects class. But there are also variables accessible by all instances of a classthese are called class variables. Variables can also be shared across multiple classes that are not part of the same class hierarchy subtree. These variables are called pool variables. The special dictionary Smalltalk contains global name/value pairs for all classes and pool dictionaries.
The Smalltalk-80 class hierarchy factors descriptions of objects into various levels of detail, anticipating further research into kinds of classes. The important point to remember is that the choice of class hierarchy, including how metaclasses work, is part of the runtime environment and not inherent in the language semantics. The irony is that in an effort to simplify the system, we managed to produce a more complex class library.
Moreover, because the tools did not reflect the flexibility that the metaclass protocol offered, some interesting opportunities were lost. In fact, having class Class as an instance of Metaclass demonstrates that Smalltalk-80 supported instance-specific behavior. No tools made it obvious, but other instances of Metaclass could have been created offering alternative instance-specific behavior.
The definition for the Smalltalk-80 language system has not changed much since its first publication in the Byte Magazine of August 1981. The only major change was in creating full closure for blocks, which is basically a Smalltalk version of lambda expressions. This addition has proven useful to Smalltalk developers creating systems that dynamically create and bind behavior to objects. The full definition for Smalltalk-80, including changes proposed by the Smalltalk ANSI group, is provided in Chapter 4, Programming with Smalltalk.
Here we revisit the Box class example, this time written in Smalltalk-80:
Model subclass: #Box instanceVariableNames: size direction position classVariableNames: Box methodsFor: initialization! initialize super initialize. size := 50. position := 0 @ 0. direction := 0. Box methodsFor: accessing! position ^position position: aPoint position := aPoint direction ^direction size ^size graphicsContext ^Screen default Box methodsFor: action! move: dist rad x y | rad := self direction degreesToRadians. x := self position x. y := self position y. self position: x + (dist * rad sin) rounded @ (y - (dist * rad cos)) rounded. Box methodsFor: display! hide self graphicsContext clear. show self displayOn: self graphicsContext displayOn: surface surface displayPolygon: self relativeVertices at: self position Box methodsFor: private! relativeVertices A box is drawn by computing the coordinates of its four vertices relative to the actual position of the box. | arr angle rad p | p := 0 @ 0. angle := self direction + 270. arr := OrderedCollection new. arr add: p. 3 timesRepeat: [ angle := angle + 90. rad := angle degreesToRadians. p := p + (self size * rad sin @ (self size * rad cos negated)). arr add: p rounded]. ^arr asArray
The only unusual part of this code is that we assume a version of Smalltalk-80 with a drawing window on the display screen accessed by asking a class object Screen for its current graphics display context (Screen default). Note that the example is presented in a stylized form because (as with Smalltalk-76) there is no Smalltalk-80 syntax for linear presentation of a class description (except of course for the file format, full of exclamation points as used in the example).
For virtual memory, one of the Smalltalk-80 implementations used a new design called LOOM (large object-oriented memory). OOZE was limited by its ability to handle only 64KB objects and only 245 classes. LOOM was Ted Kaehlers design for a faster virtual memory system (Kaehler, 1986; Kaehler & Krasner, 1984). LOOM accesses objects that are in memory by indexing a table (similar to how the language interpreter works) rather than hashing into the resident object table, the way OOZE worked. As with OOZE, LOOM swaps objects to the disk and operates without assistance from the programmer.
Previous | Table of Contents | Next |