Re: Should (boundp object) be tested and what would it mean

Jean Louis <[email protected]>
Newsgroups gmane.emacs.help
Organization GNU Support
Message-ID <[email protected]>
On 2026-04-12 22:33, Heime wrote:
> Have made this function This function to tell me the symbol name
> of the type of object being passed.
> 
> Should one test for (boundp object) and what would that mean exactly?
> 
> (defun lana-obnam (object)
>   "Return variable/symbol NAME holding OBJECT."
>   (cond
>     ((symbolp object)  (symbol-name object))
> 
>     ;;((boundp object)  (something))
> 
>     ((hash-table-p object)  "hash-table")
>     ((plistp object)        "plist")
> 
>     (t  (format "%s" object))))

- boundp tests whether a symbol has a value bound to it
- boundp expects a symbol as argument, not just any object.

(setq myvar 42) ⇒ 42     ;; Now myvar is bound
(boundp 'myvar) ⇒ t     ;; → t

(setq myvar nil) ⇒ nil    ;; Still bound (has value nil)
(boundp 'myvar) ⇒ t     ;; → t

(makunbound 'myvar) ⇒ myvar ;; Unbind it
(boundp 'myvar) ⇒ nil     ;; → nil

(defun lana-obnam (object)
   "Return description of OBJECT.
If OBJECT is a symbol, return its name.
If OBJECT is a bound symbol, also indicate it has a value.
Otherwise return the type or value as string."

   (cond
     ((symbolp object)
      (if (boundp object)
          (format "symbol '%s (value: %s)" object (symbol-value object))
        (format "symbol '%s (unbound)" object)))

     ((hash-table-p object)  "hash-table")
     ((plistp object)        "plist")
     (t  (format "%s" object))))

(setq myvar 1) ⇒ 1
(lana-obnam 'myvar) ⇒ "symbol 'myvar (value: 1)"   ;; Pass the SYMBOL 
myvar
(lana-obnam myvar) ⇒ "1"    ;; Pass the VALUE of myvar

-- 
Jean Louis
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.