boundp
boundp Function
Syntax:
boundp symbol → generalized-boolean
Arguments and Values:
symbol—a symbol.
generalized-boolean—a generalized boolean.
Description:
Returns true if symbol is bound; otherwise, returns false.
Examples:
(setq x 1) → 1
(boundp ’x) → true
(makunbound ’x) → X
(boundp ’x) → false
(let ((x 2)) (boundp ’x)) → false
(let ((x 2)) (declare (special x)) (boundp ’x)) → true
Exceptional Situations:
Should signal an error of type type-error if symbol is not a symbol.
See Also:
set, setq, symbol-value, makunbound
Notes:
The function bound determines only whether a symbol has a value in the global environment; any lexical bindings are ignored.
Expanded Reference: boundp
Basic usage
boundp returns true if the symbol has a global (dynamic) value, and false otherwise.
(defvar *my-var* 42)
(boundp '*my-var*)
=> T
(boundp (gensym))
=> NIL
After makunbound
makunbound removes the value binding, so boundp returns false afterwards.
(setf (symbol-value 'temp-var) 100)
(boundp 'temp-var)
=> T
(makunbound 'temp-var)
(boundp 'temp-var)
=> NIL
boundp does not see lexical bindings
boundp only checks the global/dynamic environment, not lexical bindings.
(makunbound 'lex-test)
(let ((lex-test 99))
(boundp 'lex-test))
=> NIL
boundp sees dynamic (special) bindings
When a variable is declared special, boundp can see its dynamic binding.
(makunbound 'dyn-test)
(let ((dyn-test 99))
(declare (special dyn-test))
(boundp 'dyn-test))
=> T
Safe value access pattern
boundp is commonly used to safely access a symbol's value without risking an error.
(defun safe-value (sym &optional default)
(if (boundp sym)
(symbol-value sym)
default))
(setf (symbol-value 'configured) :yes)
(safe-value 'configured)
=> :YES
(safe-value (gensym) :not-set)
=> :NOT-SET
Keywords and constants are always bound
(boundp :any-keyword)
=> T
(boundp t)
=> T
(boundp nil)
=> T
(boundp 'pi)
=> T