Let us finally deal with the instantiation of classes. In this section we are only concerned with instantiation of non-metaclasses. Recall that a meta class is supposed to be instantiated only once, via the use of the primitive new-instance .
As already mentioned, an important goal of having metaclasses at all is to be able to instantiate classes by sending messages to the objects that represent classes. If A refers to the meta-object, which represents the class A, the following shows how we want to instantiate A.
(define an-A (send A 'new))
The natural place of the method, which responds to the message new , is in class-class , because it is a superclass of all metaclasses. Let me explain what happens in the method new
of class class during the instantiation (send A 'new) . The method new is defined in the following way:
(define (new) (let ((instance (new-instance (method-lookup self 'instantiator)))) (set! instances (cons instance instances)) instance))
Figure: The parts of an A class and messages involved in its instantiation.
Figure shows the parts of the object, to which the new
message is sent. Self
refers to the most specific part of the
object, because we are based on the interpretation of
self
from section (virtual-like methods).
Because of this interpretation of self
, the instantiator passed
to new-instance
is that belonging to the metaclass of A. Next,
new-instance
allocates the new instance, and it arranges for
virtual operations of the object. The assignment of instances
puts the newly allocated instance into a list of instances, which is kept as a class variable of class-class . Finally the new instance is returned.
Figure also shows the list of messages that are involved in the
creation of a new A object (relative to figure
).
The first category of messages locates and returns the method
new
of class-class
. The second group of messages stems
from the actviation of new-instance
. The instantiators of
A-class
, B-class
, as well as object-class
are
located and used during the instantiation. Finally new-instance sends
the message set-self!
to the new object. This starts the
already described chain reaction of
set-self!
messages (see section
).