From: chenyu
Subject: newbie question: "defstruct" and modify structure
Date: 
Message-ID: <6143ac23.0312010253.4d7fb089@posting.google.com>
hi,
The book of "Common lisp - A gentle introduction to symbolic
computation" has the following example which is used to show how to
change structure definition. But I failed to try it.

(defstruct starship
    (name nil)
    (speed 0 )
    (condition 'green)
    (shields 'down))

(setf s3 (make-starship :captain  "benson"))=>
Error: The passed key :CAPTAIN is not defined for this function
(I just want to add new slot "captain").


Thank you for your attention.
kind regards/chenyu
From: Pascal Costanza
Subject: Re: newbie question: "defstruct" and modify structure
Date: 
Message-ID: <bqf76d$16m4$1@f1node01.rhrz.uni-bonn.de>
chenyu wrote:

> hi,
> The book of "Common lisp - A gentle introduction to symbolic
> computation" has the following example which is used to show how to
> change structure definition. But I failed to try it.
> 
> (defstruct starship
>     (name nil)
>     (speed 0 )
>     (condition 'green)
>     (shields 'down))
> 
> (setf s3 (make-starship :captain  "benson"))=>
> Error: The passed key :CAPTAIN is not defined for this function
> (I just want to add new slot "captain").

You first have to change the definition of the structure before you can 
create objects that contain new fields. So first change the defstruct 
starship above to include a captain field, and then try again.

BTW, if you change a structure definition, the consequences of accessing 
old objects of that structure is undefined. So don't expect that you can 
change objects on the fly.

That's what the Common Lisp Objects System (defclass and friends) is 
designed for, and most of the time you don't need defstruct but better 
use defclass.


I hope this helps.

Pascal

-- 
Pascal Costanza               University of Bonn
···············@web.de        Institute of Computer Science III
http://www.pascalcostanza.de  R�merstr. 164, D-53117 Bonn (Germany)