From sven.emtell at doremir.com Tue May 10 13:52:58 2011 From: sven.emtell at doremir.com (Sven Emtell) Date: Tue, 10 May 2011 15:52:58 +0200 Subject: [cl-store-devel] serialized class instance as document format Message-ID: <82425AB0-607C-442F-93A2-D4E2AD659D0D@doremir.com> Hi all I am planning to use cl-store to save and load documents in an application that I am developing. What I want to save is a serialized class instance using cl- store:store and then later be able to load it using cl-store:restore. What troubles me is that in future versions of my application, there may be new slots in the class. My tests reveal that such new slots end up being unbound when loading an old class instance (without the slot). Instead I would like it to have the new slot set to it's :initform value. Is this possible? Any thoughts on using a serialized class instance as document format? Thanks! Sven From alex.mizrahi at gmail.com Tue May 10 17:02:08 2011 From: alex.mizrahi at gmail.com (Alex Mizrahi) Date: Tue, 10 May 2011 20:02:08 +0300 Subject: [cl-store-devel] serialized class instance as document format References: <82425AB0-607C-442F-93A2-D4E2AD659D0D@doremir.com> Message-ID: <7CD6F7AAB1B04AAEBA9CF3A96535908C@killer> hello SE> I am planning to use cl-store to save and load documents in an application that I am developing. What I want to save is a serialized class instance using cl- store:store and then later be able to load it using cl-store:restore. What troubles me is that in future versions of my application, there may be new slots in the class. My tests reveal that such new slots end up being unbound when loading an old class instance (without the slot). Instead I would like it to have the new slot set to it's :initform value. Is this possible? I'm not sure about cl-store specific solution, but here's how you can auto-initialize (on demand) any unbound slots: (defclass my-persistent-object () ()) (defmethod slot-unbound (class (instance my-persistent-object) name) ;; when we've got unbound slot ;; try reinitializing it with initform via shared initialize (shared-initialize instance (list name)) (if (slot-boundp instance name) ;; if it becomes bound, call slot-value once again. ;; (I hope it does not get into loop.) (slot-value instance name) ;; otherwise call next method which signals error (call-next-method))) Then make my-persistent-object parent for all classes which need this init-on-demand behaviour. I never use unbound slots anywhere in a normal course of a program (I use NIL if something does not exist), so this works fine. Even if you use unbound slots it is unlikely that you actually want unbound slot condition to be signalled... From sven.emtell at doremir.com Tue May 10 17:37:32 2011 From: sven.emtell at doremir.com (Sven Emtell) Date: Tue, 10 May 2011 19:37:32 +0200 Subject: [cl-store-devel] serialized class instance as document format In-Reply-To: <7CD6F7AAB1B04AAEBA9CF3A96535908C@killer> References: <82425AB0-607C-442F-93A2-D4E2AD659D0D@doremir.com> <7CD6F7AAB1B04AAEBA9CF3A96535908C@killer> Message-ID: <4C3D7DAA-BD83-49B3-BD8E-19ACB98439E4@doremir.com> Thanks Alex! For me I think it suffices using "(shared-initialize t)", then all unbound slots will be nicely initialized with their :initform value. I didn't know how the initialization worked - thanks for pointing me in the right direction. Thoughts on using a serialized class instance as document format, anyone? /Sven 10 maj 2011 kl. 19.02 skrev Alex Mizrahi: > hello > > SE> I am planning to use cl-store to save and load documents > in an > application that I am developing. > What I want to save is a serialized class instance using cl- > store:store and then later be able to load it using cl- > store:restore. > What troubles me is that in future versions of my application, > there > may be new slots in the class. My tests reveal that such new > slots end > up being unbound when loading an old class instance (without the > slot). Instead I would like it to have the new slot set to > it's :initform value. > Is this possible? > > > I'm not sure about cl-store specific solution, but here's how you can > auto-initialize (on demand) any unbound slots: > > (defclass my-persistent-object () ()) > > (defmethod slot-unbound (class (instance my-persistent-object) name) > ;; when we've got unbound slot > ;; try reinitializing it with initform via shared initialize > (shared-initialize instance (list name)) > (if (slot-boundp instance name) > ;; if it becomes bound, call slot-value once again. > ;; (I hope it does not get into loop.) > (slot-value instance name) > ;; otherwise call next method which signals error > (call-next-method))) > > Then make my-persistent-object parent for all classes which need this > init-on-demand behaviour. > > I never use unbound slots anywhere in a normal course of a program > (I use > NIL if something does not exist), so this works fine. > > Even if you use unbound slots it is unlikely that you actually want > unbound > slot condition to be signalled... > > > _______________________________________________ > cl-store-devel mailing list > cl-store-devel at common-lisp.net > http://common-lisp.net/cgi-bin/mailman/listinfo/cl-store-devel