Emacs Lisp: Symbol vs String for non-nil Values

By Xah Lee. Date: . Last updated: .

Which of the following should you use?

(search-forward "cat" nil "NOERROR" )

(search-forward "cat" nil 'NOERROR )

Using symbol is problematic. It adds the symbol to the symbol lookup table obarray , and may already have a value, possibly of nil.

emacs symbols obarray list 2017 01 06
emacs run as --batch, not loading anything, there are 15305 symbols in obarray. normally, it's 45k to 80k symbols or more, depending number of packages loaded.

Here's explanation.

In elisp:

〔➤see Emacs Lisp: Function Optional Parameters

So, this create a situation. When the parameters are meant to be true and false, you have code like this: (f t nil t t nil nil), which is hard to understand.

For example, the search-forward function:

(search-forward STRING &optional BOUND NOERROR COUNT)

and you may call it like this:

(search-forward "cat" nil t )

which means, search for “cat”, to the end of buffer (not bounded), and do not report error if not found (and move cursor to the found text or end of search limit (e.g. end of buffer) if none found.).

One way to make it more readable is to replace some t value by a string, since string in elisp is also non-nil. (Warning: some function's parameter differentiate t and other non-nil values, so you can't always just pass it a string without consideration. For example, search-forward's 3rd parameter and replace-match's 4th parameter. )

You can make a non-nil value more readable by passing a string instead, like this:

(search-forward "cat" nil "NOERROR" )

A couple years ago (~2015) a elisp hacker suggested to me to replace string "NOERROR" by a symbol 'NOERROR. The reason given was that it costs more to create a string than symbol.

I didn't really agree, because:

However, i sometimes went along, just to experiment. So, in some parts of my code, you'll see 'NOERROR and in other parts you see "NOERROR".

Today, i'm changing all of them back to the string form "NOERROR".

Here's the functions that changed.

Like it? Buy Xah Emacs Tutorial.

or, buy something from Best Keyboard for Emacs