4

According to the Emacs Lisp manual http://www.gnu.org/software/emacs/manual/html_node/elisp/Symbol-Type.html

A symbol whose name starts with a colon (‘:’) is called a keyword symbol. These symbols automatically act as constants, and are normally used only by comparing an unknown symbol with a few specific alternatives.

and http://www.gnu.org/software/emacs/manual/html_node/elisp/Property-Lists.html#Property-Lists

A property list (plist for short) is a list of paired elements. Each of the pairs associates a property name (usually a symbol) with a property or value.

What is the difference between:

(let ((my-plist '(bar t foo 4)))
  (plist-get my-plist 'foo))

and

(let ((my-plist '(bar t :foo 4)))
  (plist-get my-plist :foo))

and which one is preferred?

  • 1
    I guess you mean '(:bar t :foo 4) in the second example. – Lindydancer May 5 '15 at 9:51
3

There's no difference to my knowledge. You can see :foo as an auto-quoting 'foo. Keyword arguments show more clearly that they are constants, so you should use them if you want to do that.

You can also experiment with this if you like:

(let (cands)
  (mapatoms (lambda (x)
              (when (string-match "foobarbaz" (symbol-name x))
                (push x cands))))
  cands)

The initial eval should return nil. But after you eval (list :foobarbaz), it will change. After you eval (list 'foobarbaz), it will change once more.

  • Thanks, then I will use keyword symbols in property lists since it makes it more clear what is the property name and what is its value.. – Håkon Hægland May 5 '15 at 9:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.