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.
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 likerecord-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 theinitial-value
orinitializer
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 toinitialize-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 symbolno-initialize-instance
, then the constructor accepts no extra arguments and does not callinitialize-instance
at all; this is desirable wheninitialize-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 forno-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)))
Returns the class of instance. This is faster than
object-class
, but it works only for instances, and not for other objects.