7

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?

Drew
  • 75,699
  • 9
  • 109
  • 225
Håkon Hægland
  • 3,608
  • 1
  • 20
  • 51

1 Answers1

5

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.

abo-abo
  • 13,943
  • 1
  • 29
  • 43
  • 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 05 '15 at 09:50