[CM] Saving LISP objects to disk

Heinrich Taube taube at uiuc.edu
Tue, 25 Mar 2008 08:06:52 -0500


you can define methods on make-load-form to do just this. you still  
have to traverse the slots by hand but thats not so bad since you can  
use the MOP functions to do this. your code would need special  
knowlege of which slots to recusively process but thats it.

(import '(sb-pcl:slot-definition-initargs
           sb-pcl:slot-definition-initform
           sb-pcl:slot-definition-name
           sb-pcl:class-direct-slots
           sb-pcl:class-slots
           sb-pcl:class-direct-superclasses
           sb-mop:class-direct-subclasses
           )

(defmethod make-load-form ((obj foobar) )
   (let (inits ())
     (setf inits (loop for slot in (class-slots (class-of obj))
                       for name = (slot-definition-name slot)
                       if (slot-boundp obj name)
                         collect (car (slot-definition-initarg slot)
                         and collect (slot-value obj name)))
     `(make-instance (quote ,(class-name (class-of obj)))
                      ,@ inits)))



> One thing that has cropped up as I am trying to resurrect my PICACS  
> code in SBCL is that the code I developed to save objects (and any  
> subobjects) to disk is broken, because of all of the CCL calls I used.
>
> I there a standard LISP way of writing objects to disk (and later  
> restoring them), including all subobjects?
>
> As it was, I solved it three years ago by writing code that  
> determines the slots of any object, traverses the whole object and  
> subobject tree of a given object and writes out to disk the code  
> needed to recreate the objects.
>
> Surely there is a better way, and one that doesn't require system- 
> specific calls (?) -- and three days of hunting poor documentation  
> to figure out how to translate my CCL implementation to SBCL?
>
> Cheers,
> -=Bret
>