Changes to static slot inheritance.

 new new list compose Reply to this message Top page
Attachments:
+ (text/plain)
+ (text/html)

Delete this message
Author: Kaz Kylheku
Date:  
To: TXR Users
Subject: Changes to static slot inheritance.

Hi all,

I'm making some changes to the OOP structures in TXR Lisp in the area of static slots.

Currently when a new struct type is derived from an existing one, although it inherits the static slots, it has its own global instance of them. Concretely, given:

(defstruct animal () (:static legs 4))

(defstruct octopus animal)

if we construct a (new octopus) and capture it as variable o, then o.legs evaluates to 4. If we then evaluate (set o.legs 8), only the octopus version of the legs static slot changes. All octopus instances have 8 legs now, but animal instances are not affected.

Under the new change, octopus would share the slot with animal, which seems more correct. I.e. inherit the actual slot, not just the fact that there is to be such a slot with such a value.

To obtain the behavior of octopus having its own legs slot, we now do this:

(defstruct octopus animal (:static legs 8))

this is now a separately instantiated static slot for octopus only. If the value is not specified, it is nil; I might arrange for a default value to come from the base slot. This aspect isn't finalized.

There are also changes to the static-slot-ensure funtion, which is the basis for defmeth.   Currently, it walks  the entire inheritance tree rooted at a given type and either creates or updates the given slot. Going forward, the recursive walk will stop when a non-static slot of that name is encountered, or a static slot which is not shared with the parent. So above, if we were to evaluate (static-slot-ensure 'animal 'legs 100), it will not affect the legs slot of octopus.

This behavior will fix bug with method redefinitions.  Methods are static slots. If a derived struct has its own override of a method, a redefinition of the base method must not clobber it. Currently, that is broken:

$ txr
This is the TXR Lisp interactive listener of TXR 151.
Use the :quit command or type Ctrl-D on empty line to exit.
1> (defstruct dog () (:method speak (self) "bark"))
#<struct-type dog>
2> (defstruct toy-poodle dog (:method speak (self) "yelp"))
#<struct-type toy-poodle>
3> (new toy-poodle).(speak)
"yelp"
4> (defmeth dog speak (self) "woof")
(meth dog speak)
5> (new toy-poodle).(speak)
"woof"

Oops! What happened to toy-poodle's speak method? It was wrongly clobbered.

The new static slot semantics and behavior of static-slot-ensure fix this: the toy-poodle continues to "yelp" even though dog's speak method changed "woof".