Re: Determine object passed as argument
Jean Louis <[email protected]>
| Newsgroups | gmane.emacs.help |
|---|---|
| Organization | GNU Support |
| Message-ID | <[email protected]> |
On 2026-04-11 17:25, Heime wrote: > On Sunday, April 12th, 2026 at 12:57 AM, Jean Louis <[email protected]> > wrote: > >> On 2026-04-11 15:14, Heime wrote: > >> > Can a function determine the object being passed as argument and >> > print its name. For instance that I passed if I pass fpln when >> > calling (lana-du-fpln 'fpln). >> > >> > (defun lana-du-fpln (datenstruk &optional bname) >> >> In Emacs Lisp, you cannot directly get the variable name that was >> passed >> to a function, because arguments are evaluated before being passed. By >> the time datenstruk receives the value, the original variable name is >> lost. >> >> (defun lana-du-fpln (datenstruk-symbol &optional bname) >> "Print the name of the symbol passed, then access its value." >> (message "Processing variable: %s" datenstruk-symbol) >> (let ((datenstruk (symbol-value datenstruk-symbol))) >> ;; Now work with datenstruk's value >> (message "Value: %S" datenstruk))) >> >> Call it with quoted symbol: (lana-du-fpln 'fpln) > > The above gives "Warning: lana-fpln is unbound (void variable)" that is correct, it is for you to do something with it. >> (defun lana-du-fpln (datenstruk-symbol &optional bname) >> "Print the name of the symbol passed, then access its value if >> bound." >> (message "Processing variable: %s" datenstruk-symbol) >> (if (boundp datenstruk-symbol) >> (let ((datenstruk (symbol-value datenstruk-symbol))) >> (message "Value: %S" datenstruk) >> ;; Your logic here >> ) >> (message "Warning: %s is unbound (void variable)" >> datenstruk-symbol))) >> >> "Warning: fpln is unbound (void variable)" > > The above gives "Processing variable: fpln" followed by its value. > > Why does the condition (if (boundp datenstruk-symbol) make things work > out? The boundp check prevents the symbol-value call from throwing a "void variable" error when the symbol hasn't been defined, allowing the function to handle unbound variables gracefully instead of crashing. -- Jean Louis