Next: , Previous: Classes, Up: Top


2 Instances

An instance is a compound data structure much like a record, except that it is defined by a class rather than a record type descriptor. Instances are more powerful than records, because their representation is designed to support inheritance, while the representation of records is not.

— Procedure: instance-constructor class slot-names [n-init-args]

Creates and returns a procedure that, when called, will create and return a newly allocated instance of class.

Class must be a subclass of <instance>. Slot-names must be a list of symbols, each of which must be the name of a slot in class. N-init-args will be described below.

In its basic operation, instance-constructor works much like record-constructor: the slot-names argument specifies how many arguments the returned constructor accepts, and each of those arguments is stored in the corresponding slot of the returned instance. Any slots that are not specified in slot-names are given their initial values, as specified by the initial-value or initializer slot properties; otherwise they are left uninitialized.

After the new instance is created and its slots filled in, but before it is returned, it is passed to the generic procedure initialize-instance. Normally, initialize-instance does nothing, but because it is always called, the programmer can add methods to it to specify an initialization that is to be performed on every instance of the class.

By default, initialize-instance is called with one argument, the newly created instance. However, the optional argument n-init-args can be used to specify additional arguments that will be passed to initialize-instance.

The way this works is that the returned constructor procedure accepts additional arguments after the specified number of slot values, and passes these extra arguments to initialize-instance. When n-init-args is not supplied or is #t, any number of extra arguments are accepted and passed along. When n-init-args is an exact non-negative integer, exactly that number of extra arguments must be supplied when the constructor is called. Finally, if n-init-args is the symbol no-initialize-instance, then the constructor accepts no extra arguments and does not call initialize-instance at all; this is desirable when initialize-instance is not needed, because it makes the constructor significantly faster.

For notational convenience, n-init-args may take two other forms. First, it may be a list of symbols, which is equivalent to the integer that is the length of the list. Second, it may be the symbol no-init, which is an abbreviation for no-initialize-instance.

Note that the default method on initialize-instance accepts no extra arguments and does nothing.

Examples of instance-constructor:

          (define-class <simple-reference> (<reference>)
            (from accessor reference-from)
            (to accessor reference-to)
            (cx accessor reference-cx)
            (cy accessor reference-cy))
          
          (define make-simple-reference
            (instance-constructor <simple-reference>
                                  '(from to cx cy)
                                  'no-init))
          
          (define-class <simple-wirenet> (<wirenet>)
            (cell accessor wirenet-cell)
            (wires accessor wirenet-wires
                   modifier set-wirenet-wires!
                   initial-value '()))
          
          (define make-simple-wirenet
            (instance-constructor <simple-wirenet> '(cell)))
     
— Procedure: instance? object

Returns #t if object is an instance, otherwise returns #f.

— Procedure: instance-class instance

Returns the class of instance. This is faster than object-class, but it works only for instances, and not for other objects.

— Procedure: instance-of? object specializer

Returns #t if object is a general instance of specializer, otherwise returns #f. This is equivalent to

          (subclass? (object-class object) specializer)
     
— Procedure: instance-predicate specializer

Returns a predicate procedure for specializer. The returned procedure accepts one argument and returns #t if the argument is an instance of specializer and #f otherwise.