Inheritance of Slots and Slot Options (original) (raw)
ANSI Common Lisp 7 Objects
7.5 Slots
7.5.3 Inheritance of Slots and Slot Options
The set of the names of all slots _accessiblein an instance of a class C is the union of the sets of names of slots defined by C and its_superclasses. The structure of an instance is the set of names of local slots in that instance.
In the simplest case, only one class among C and its _superclasses_defines a slot with a given slot name. If a slot is defined by a superclass of C, the slot is said to be inherited. The characteristics of the slot are determined by the _slot specifier_of the defining class. Consider the defining class for a slot S. If the value of the :allocationslot option is :instance, then S is a local slot and each _instance_of C has its own slot named S that stores its own value. If the value of the :allocation slot option is :class, then _S_is a shared slot, the classthat defined S stores the value, and allinstances of C can access that single slot. If the :allocation slot option is omitted, :instance is used.
In general, more than one class among C and its superclasses can define a slot with a given name. In such cases, only one slot with the given name is accessible in an _instance_of C, and the characteristics of that slot are a combination of the several _slot_specifiers, computed as follows:
- All the slot specifiers for a given slot name are ordered from most specific to least specific, according to the order in C's_class precedence list_ of the classes that define them. All references to the specificity of slot specifiers immediately below refers to this ordering.
- The allocation of a slot is controlled by the most specific slot specifier. If the most specific _slot specifier_does not contain an :allocation slot option, :instance is used. Less specific slot specifiers do not affect the allocation.
- The default initial value form for a slotis the value of the :initform slot option in the most specificslot specifier that contains one. If no _slot specifier_contains an :initform slot option, the _slot_has no default initial value form.
- The contents of a slot will always be of type (and T1 ... Tn) where T1 ... Tn are the values of the :type slot options contained in all of the_slot specifiers_. If no slot specifier contains the:type slot option, the contents of the slot will always be of type t. The consequences of attempting to store in a _slot_a value that does not satisfy the type of the slot are undefined.
- The set of initialization arguments that initialize a given slot is the union of the initialization arguments declared in the :initarg slot options in all the slot specifiers.
- The documentation string for a slot is the value of the :documentation slot option in the most specific _slot_specifier that contains one. If no slot specifier contains a:documentation slot option, the slot has no documentation string.
A consequence of the allocation rule is that a shared slot can be_shadowed_. For example, if a class C1 defines a slot named _S_whose value for the :allocation slot option is :class, that slot is _accessiblein instances of C1 and all of its_subclasses. However, if C2 is a _subclassof C1 and also defines a slot named S, C1's slot is not shared by instances of C2 and its subclasses. When a class_C1 defines a shared slot, any subclass C2 of C1 will share this single _slotunless the defclass form for_C2 specifies a slot of the same name or there is a _superclassof C2 that precedes C1 in the class precedence list of_C2 that defines a slot of the same name.
A consequence of the type rule is that the value of a _slot_satisfies the type constraint of each slot specifier that contributes to that slot. Because the result of attempting to store in a slot a value that does not satisfy the type constraint for the slot is undefined, the value in a _slot_might fail to satisfy its type constraint.
The :reader, :writer, and :accessor slot options create methods rather than define the characteristics of a slot. Reader and writer methods are inherited in the sense described in Section 7.6.7 Inheritance of Methods.
Methods that access slots use only the name of the_slot_ and the type of the slot's value. Suppose a superclass provides a method that expects to access a_shared slot_ of a given name, and a subclass defines a local slot with the same name. If the method provided by the superclass is used on an instance of the subclass, the method accesses the local slot.