Elisp: Symbol Cells

By Xah Lee. Date: . Last updated: .

Symbol's Cells

Each Symbol has the following “cells” to store things:

print name cell
  • A string, the same as the symbol.
  • Automatically set, cannot be changed.
value cell
  • Store the symbol's value.
  • When value cell is not void, the symbol is considered as a variable.

You can check if a symbol's value cell is not empty, by boundp. (we think of it as checking if a variable is defined.)

(boundp 'xx)
function cell
  • Store function definition object, lisp macros, or other objects that can act as function.

You can check if a symbol's function cell is not empty, by fboundp.

(fboundp 'somef)

💡 TIP: Because a symbol can both hold a value and a function, a symbol can be both a variable and function. For example, the symbol buffer-file-name is both a variable and function. (you can try describe-function and describe-variable on it.)

property list cell
  • Hold a list of name and value pairs.
  • Used for storing meta info about the symbol, such as function state, font face spec (for syntax coloring), deprecation flag, etc.

A symbol's value cell or function cell may be empty. If so, it's said to be “void”. When you try to get a cell's value that's void, it's a lisp error. (a empty cell is not the same as having value of nil.)

💡 TIP:

Normally, you don't need to worry about any of these tech details. The only thing that's useful for most elisp code is property list.

Get Value of Symbol's Cells

symbol-name
get symbol's name cell value
(symbol-name 'sin) ; "sin"
symbol-value
get symbol's value cell value
(symbol-value 'sin) ; void-variable error
;; because the value cell of the symbol sin is void
;; or, we just say that sin isn't a variable
symbol-function
get symbol's function cell value
(symbol-function 'sin) ;#<subr sin>
;; the value is a primitive function (written in C), and has print form of #<subr sin>
symbol-plist
get symbol's property list cell value
(symbol-plist 'sin) ; (side-effect-free t)

Set Symbol's Name Cell

Symbol's name cell is automatically set, as a string of the symbol name. Symbol name cell cannot be changed.

Set Symbol's Value Cell

The normal way to set a symbol's value cell is using setq.

;; set a symbol's value cell
(setq y "yes yes")

;; get it
(symbol-value 'y) ; "yes yes"

Set Symbol's Function Cell

The normal way to set a symbol's function cell is using defun or defmacro.

;; a function that returns 4
(defun z () 4)
;; Note: return value of defun is not defined

;; get a symbol's function cell value
(symbol-function 'z) ; (lambda nil 4)

Set Symbol's Property List Cell

Elisp, symbol