help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: plist-put modification by side effect


From: Dan Davison
Subject: Re: plist-put modification by side effect
Date: Sun, 1 Feb 2009 12:11:28 -0500
User-agent: Mutt/1.5.18 (2008-05-17)

On Sun, Feb 01, 2009 at 11:23:14AM -0500, Dan Davison wrote:
> On Sun, Feb 01, 2009 at 12:02:42AM +0100, Thien-Thi Nguyen wrote:
> > () Dan Davison <davison@stats.ox.ac.uk>
> > () Sat, 31 Jan 2009 16:06:30 -0500
> > 
> >    I don't get this. It says the plist is altered by side
> >    effects. So what's with the "but just to be extra careful
> >    use (setq ...)"  advice?
> > 
> > If PROP is in PLIST, the natural side-effecting operation is
> > to splice in the VALUE at the position of the old value.  Ok,
> > fine.  Now, what if PROP is not in PLIST?  How might you
> > implement that?  How might you implement that, differently?
> > How might your design decision surprise the unwary caller?
> > 
> 
> Thanks Thi and Helmut for your answers.
> 
> Let me see if can answer the questions Thi set me. If PROP is not in PLIST,
> then I guess the two possibilities you are referring to are 
> 
> 1. add PROP with value VAL to head of PLIST
> 2. add PROP with value VAL to tail of PLIST
> 
> and only in case 2 will PLIST be altered by side-effect.

I want to modify a plist inside a function, in such a way that
the modifications are available outside the function. Does Thi's
answer mean that I should use something like nconc and not
plist-put?

For example:

;; Construct a plist in the global environment
(setq val1 "value 1" val2 "value 2")
(setq options (list :opt1 val1 :opt2 val2))
;; Is this a correct way to construct a plist? I can't find this
;; discussed in the GNU Emacs Lisp Reference. Could someone point me
;; towards documentation of the `:key val' syntax?

;; And here's a function that I'm hoping will modify the plist in the
;;   global environment:
(defun f (opts)
  (setq val3 "value 3")
  (plist-put opts :opt3 val3)) ;; don't care about return value of plist-put

(f options)

;; As a matter of fact, when I did this, options does now have :opt3,
;; but from what Thi said it sounds like I can't rely on that. So what
;; is the correct thing to do? Should I use

(defun g (opts)
  (setq val3 "value 3")
  (nconc opts (list :val3 val3)))

?

Thanks again,

Dan










> 
> So if I've understood this then I think the docstring is inaccurate
> since `The PLIST is modified by side effects.' is not necessarily
> true. I also think it would be helpful if the docstring spelled things
> out more. I'd suggest something like
> 
> Change value in PLIST of PROP to VAL.
> PLIST is a property list, which is a list of the form (PROP1 VALUE1
> PROP2 VALUE2 ...).  PROP is a symbol and VAL is any object.  If PROP
> is already a property on the list, its value is set to VAL. Otherwise
> the new PROP VAL pair is added; in this case PLIST (if non-nil)
> may be modified by side effects, although this cannot be relied
> upon. Therefore use `(setq x (plist-put x prop val))' to be sure to
> use the new value.
> 
> Dan
> 
> > thi
> > 
> 
> -- 
> http://www.stats.ox.ac.uk/~davison
> 

-- 
http://www.stats.ox.ac.uk/~davison




reply via email to

[Prev in Thread] Current Thread [Next in Thread]