Kaz Kylheku
11/26/2015 7:57:00 PM
On 2015-11-26, Jim Newton <jimka.issy@gmail.com> wrote:
> This is something surprising I learned today.
>
> (defclass A ()
> ((s :type X)))
>
> (defclass B ()
> ((s :type Y)))
>
> The resulting type restriction on slot s in class b is NOT Y, but rather (AND
> X Y).
How so? A and B are not related in any way. Maybe you wanted
(defclass B (A) ...)
> This means that a subclass is sadly not allowed to relax a type restriction.
Why would it? It violates OOP.
Above, we have defined B as *inheriting* from A.
So, by golly, that's what happened: B inherited the slot s, along
with that slot's restriction.
If B derives from A, then each instance of B *is an* A.
If what it means to be an A is to have a slot restricted to type X,
and B doesn't exhibit that restriction, then a B is not an A.
Places in the program which work with A expect the slot restriction to hold,
and if a B is substituted, it will create a surprise.
That is okay; in CLOS we don't have to derive B from A in order to
substitute instances of them in the same places. We don't even have
to derive them from the same base. They can be totally independent and just
repeat some of the common slots.
To reduce the repetition we can factor out what is common to B and A into a
common base, and just introduce the slot s differently in A and B.
> This is inkeeping with the spec. indeed see 7.5.3 Inheritance of Slots and
> Slot Options
By the way, is the substitution principle broken by the fact that we can change
the slot's :allocation in a derived class?
Suppose I make a class A in which slot s is ":allocation :class".
The Devil's Advocate argument can be made that what it means for an object to
be an A is that it shares the same value of s with all other instances.
Therefore if B is derived from A and the :allocation of s is changed to
:instance, we are breaking the principle: code which has two distinct A
objects (that are actually B) will be surprised that their s slots are not
equal.
I can't entirely reject this argument, but at the same time, overriding slot
allocation makes possible some useful and seemingly benign approaches.