Best way to set object slots when loading from file?
From: Björn Lindberg (d95-bli_at_nada.kth.se)
Date: 12/19/03
- Next message: Kalle Olavi Niemitalo: "Re: Where to find good lisp critiques?"
- Previous message: james anderson: "Re: A brash newbie question on macrology"
- Next in thread: Rahul Jain: "Re: Best way to set object slots when loading from file?"
- Reply: Rahul Jain: "Re: Best way to set object slots when loading from file?"
- Reply: Wade Humeniuk: "Re: Best way to set object slots when loading from file?"
- Reply: Thomas F. Bur***: "Re: Best way to set object slots when loading from file?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 19 Dec 2003 00:11:35 +0100
Consider the following class:
(defclass foo ()
((alpha :initarg :alpha :accessor alpha)
(beta :initarg :beta :accessor beta)))
Now, I want to initialise an instance of that class from a file. At
first, I simply chose the following format for my file:
(foo :alpha "A" :beta "B")
(...)
(...)
Then I could READ one object at a time, and just do
(apply #'make-instance read-object) for each instance. Now, for
various reasons, I want to change my file format to the following:
(foo (alpha "A")
(beta "B"))
(... etc ...)
Ie, more markup-language like, and I am pondering the best way to
initialise the slots. I can think of a couple of different ways:
1) Convert the first symbol in each subelement (eg alpha, beta) to one
in the keyword package, append the subelement lists, and then do as
above. This seems a bit ugly, but is the only way I can see to
initialise the slots at the same time as creating the object.
2) First create an "empty" instance, and then do
(setf (slot-value instance slot) value) for each subelement, where
slot = {alpha, beta}, and value = {"A", "B"} in turn. This has the
disadvantage that it breaks encapsulation in a way, since
now the slots are locked. If I later would like to change "alpha"
so that it is computed instead of being a slot, I cannot.
3) Create an empty instance, and then for each subelement do
(setf (accessor instance) value), where accessor is the car of the
subelement (alpha, beta), and value the cadr (in turn). Here I am
locking the names of the accessors, but it seems slightly better
than (2).
Is there a "best" way to do what I want?
Björn
- Next message: Kalle Olavi Niemitalo: "Re: Where to find good lisp critiques?"
- Previous message: james anderson: "Re: A brash newbie question on macrology"
- Next in thread: Rahul Jain: "Re: Best way to set object slots when loading from file?"
- Reply: Rahul Jain: "Re: Best way to set object slots when loading from file?"
- Reply: Wade Humeniuk: "Re: Best way to set object slots when loading from file?"
- Reply: Thomas F. Bur***: "Re: Best way to set object slots when loading from file?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]