defstruct and included slots

Discussion of Common Lisp
Post Reply
jboetje
Posts: 3
Joined: Sat Aug 16, 2008 3:53 pm

defstruct and included slots

Post by jboetje » Sun Aug 17, 2008 7:52 am

I'm cross-posting this to this forum from comp.lang.lisp in hopes of getting more discussion. Enjoy.
--------------------------
The CL spec is mute on this point. If a destruct definition includes another struct and the new struct has a slot with the name of a slot in the included struct, what happens?

1. It's an error and the new struct can't be definded.
2. The new struct masks the included struct's slot.
3. It is #2 but the accessor for the new struct will access in the new struct (ignoring the hidden original slot), and the original accessor function will access the hidden in the included struct.

Example:
(defstruct person name age)
(defstruct (astronaut (:include person)) age position)
;;; the astronaut definition is either in error (redefining age slot)
;;; or it hides the person-age (perhaps with a warning)

(setf ex-one (make-person :name "Tom" :age 29))
(setf ex-two (make-astronaut :age 27 :position 'captain))
;;; the make-astronaut is either non-existent (the defstruct was in error), or it works
;;; if we get to this point, what happens with the accessors
;;; everyone would agree that this works
(astronaut-age ex-two) => 27

;;; On the other hand, what happens if we use the person accessors
;;; is it -->
(person-age ex-two) => 27 or 29
My warped mind is inclined to allow hiding of an included slot. That would mean that
(person-age ex-two) => 29

Is there a consensus among the various CLs? thanks
Jerry

danb
Posts: 35
Joined: Sat Jun 28, 2008 1:05 pm
Location: Urbana, Illinois, US
Contact:

Re: defstruct and included slots

Post by danb » Sun Aug 17, 2008 11:36 am

jboetje wrote:I'm cross-posting this to this forum from comp.lang.lisp ... The CL spec is mute on this point. If a destruct definition includes another struct and the new struct has a slot with the name of a slot in the included struct, what happens?
Pascal C. answers the question in response #3. Looking farther down on the CLHS page, the "Exceptional Situations" section says it should signal an error.

jboetje
Posts: 3
Joined: Sat Aug 16, 2008 3:53 pm

Re: defstruct and included slots

Post by jboetje » Sun Aug 17, 2008 3:26 pm

Yes, I just got Pascal's post. Thanks!

Jerry

Post Reply