Re: Structures as literals
"David McClain (as dbm at refined-audiometrics dot com)" <[email protected]>
| Newsgroups | gmane.lisp.lispworks.general |
|---|---|
| Message-ID | <[email protected]> |
Instead of MAKE-LOAD-FORM, I use a binary interchange format for my serialization across the network and to file storage. So long as another Lisp reads them back, they are portable.
I can confirm that structure instances are binary externalizable, as long as the slots don’t contain functional closures, or point to some object that does.
But you have to work with nonportable LW-specific features.
;; ——————————————
;; DM/RAL 07/09 -- better accommodation of unkown structure classes on restore
(defstore-sdle-store (obj structure-object stream)
(output-type-code +structure-object-code+ stream)
(let* ((obj-class (class-of obj))
(class-name (class-name obj-class))
(slot-names (structure:structure-class-slot-names obj-class)))
(store-object class-name stream)
(store-count (length slot-names) stream)
(dolist (slot-name slot-names)
(store-object slot-name stream))
(dolist (slot-name slot-names)
(store-object (slot-value obj slot-name) stream))))
(defrestore-sdle-store (structure-object stream)
(let* ((class-name (restore-object stream))
(count (read-count stream))
(slot-names (loop repeat count
collect (restore-object stream)))
(class (find-or-create-class class-name 'standard-object slot-names 'standard-class)))
(cond ((eq (type-of class) 'structure-class)
;; we apparently found the class and it was a struture
(let* ((new-instance (structure::allocate-instance class))
(allowed-slots (structure:structure-class-slot-names class)))
(resolving-object (obj new-instance)
(dolist (slot-name slot-names)
(let ((val (restore-object stream))
the-slot)
(when (setf the-slot (car (member slot-name allowed-slots :test #'string-equal)))
;; slot-names are always symbols so we don't
;; have to worry about circularities
(setting (slot-value obj the-slot) val))) ))
new-instance))
(t ;; else -- no such struture known to mankind
;; dummy up as a new standard-object instead of a struct
;; (to avoid non-portability issues regarding internals of struct implementation)
(let ((new-instance (allocate-instance class)))
(resolving-object (obj new-instance)
(dolist (slot-name slot-names)
(let ((val (restore-object stream)))
(when (clos:slot-exists-p new-instance slot-name)
(setting (slot-value obj slot-name) val)) )))
new-instance))
)))
> On Mar 2, 2026, at 10:28, Tim Bradshaw (as tfb at tfeb dot org) <[email protected]> wrote:
>
> Can anyone confirm that it's the case that structure instances are by default externalizable (assuming their slots' values are of course) in LW?
>
> Reason for asking is that I was checking that my make-load-form method was right by excising it and compiling some code (in a cold LW) which would write structure instances as literals ... which I expected to fail but which worked (and loading the compiled file, again in a cold LW, does what it should).
>
> --tim
>
> _______________________________________________
> Lisp Hug - the mailing list for LispWorks users
> [email protected]
> http://www.lispworks.com/support/lisp-hug.html