[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Is it safe to modify a property list directly with PLIST-PUT?
From: |
Pascal J. Bourguignon |
Subject: |
Re: Is it safe to modify a property list directly with PLIST-PUT? |
Date: |
Sun, 26 Jul 2009 22:07:36 +0200 |
User-agent: |
Gnus/5.1008 (Gnus v5.10.8) Emacs/22.3 (darwin) |
Teemu Likonen <tlikonen@iki.fi> writes:
> I use a list of property lists to store data. It's like this:
>
> (setq my-data '((:foo "one" :bar "two")
> (:foo "three" :bar "four")))
It is never safe to modify literal data!
(setq my-data (list (list :foo "one" :bar "two")
(list :foo "three" :bar "four")))
or:
(setq my-data (copy-tree '((:foo "one" :bar "two")
(:foo "three" :bar "four"))))
> Sometimes I need to modify the data and a command like this seems to
> work:
>
> (plist-put (nth 1 my-data) :bar "New value")
>
> That is, PLIST-PUT modifies the property list and variable MY-DATA
> contains now the modified list:
>
> ((:foo "one" :bar "two")
> (:foo "three" :bar "New value"))
>
> The question: Is this reliable? Is it guaranteed that it will always
> modify the list correctly? If not, how would you suggest doing it
> instead?
It is safe, as long as the property list is not literal data (that
must be considered immutable).
Notice also that like delete, plist-put returns the result, it cannot
always modify the property list in place. So you have to restore the
result:
(setf (nth 1 my-data) (plist-put (nth 1 my-data) :bar "New value"))
> Common Lisp has so nice SETF macro...
Of course, emacs has it too:
(require 'cl) ; put that in your ~/.emacs
--
__Pascal Bourguignon__