0

I'm relatively new to Elisp and I'm trying to figure out a bug in my code, but I have no idea what to search or read in order to get an idea about it.

The bug is in this code:

(defvar some-variable "some-value")
(setq some-plist
      '(:name "name" :query some-variable :key 97))

(stringp (plist-get some-plist :query))  ;; nil (HERE)
(stringp some-variable) ;; t

Even though (plist-get some-plist :query) returns some-variable, why does stringp on it returns nil?

Drew
  • 75,699
  • 9
  • 109
  • 225
user5954246
  • 103
  • 3
  • https://emacs.stackexchange.com/tags/elisp/info – Drew Dec 27 '22 at 17:54
  • 3
    Does this answer your question? [How to evaluate the variables before adding them to a list?](https://emacs.stackexchange.com/questions/7481/how-to-evaluate-the-variables-before-adding-them-to-a-list) – Drew Dec 27 '22 at 17:56

1 Answers1

1

You need to "unquote" some-variable when building the plist. So the code should look like this:

(defvar some-variable "some-value")
(setq some-plist
      `(:name "name" :query ,some-variable :key 97))

(stringp (plist-get some-plist :query))  ;; t (HERE)
(stringp some-variable) ;; t

When you build a list using a quote ', everything is added to the list verbatim. So, going back to the snippet you posted, even though some-variable evaluates to a string, '(some-variable) is equivalent to (list 'some-variable) (a list with a quoted symbol).

To get what you are asking for, you can either use a backtick-quoted list and unquote some-variable

`(,some-variable)

or just use the list form

(list some-variable)
David Leal
  • 26
  • 4
  • 3
    Generally speaking, it is convenient to construct a property list using `list` instead of [backquoting](https://www.gnu.org/software/emacs/manual/html_node/elisp/Backquote.html), since keyword symbols are self-evaluating, so you don't need to quote them, and it is fairly common to want to evaluate the values in a plist anyway. – Philip K. Dec 27 '22 at 17:44
  • That certainly makes sense. Thanks for the tip. – David Leal Dec 28 '22 at 21:50