symbol-function vs defun question

Jeff Cunningham <[email protected]>
Newsgroups gmane.lisp.steel-bank.general
Message-ID <20240324065356.0f947782@Isabelle>
Greetings.

I have been puzzling over a difference between using SYMBOL-FUNCTION
and DEFUN within a let binding.  Here's a contrived example that
illustrates:

(let ((table1 (make-hash-table)) (table2 '(1 2 3)))
  (setf (symbol-function (intern "DESCRIBE-SOME-TABLES"))
        (lambda () (print (list :table1 table1 :table2 table2))))
  (defun a-function () (describe-some-tables)))

When this is compiled the first time, the compiler issues the warning:

;   Undefined function:
;     DESCRIBE-SOME-TABLES
;   caught 1 STYLE-WARNING condition

Yet the function works:

(a-function)
;; => (:TABLE1 #<HASH-TABLE :TEST EQL :COUNT 0 {1003B80DA3}> :TABLE2 (1 2 3))

Compiled subsequent times the warning is gone - presumably as the package namespace now knows about the function. But unbind it:

(fmakunbound 'describe-some-tables)

and recompile and the warning is back the first time.

Yet if I use DEFUN there is no such warning:

(let ((table1 (make-hash-table)) (table2 '(1 2 3)))
  (defun describe-some-tables ()
    (print (list :table1 table1 :table2 table2)))
  (defun a-function () (describe-some-tables)))

Why is this? And is there a way to get around it so I can define the
function using symbol-function? The reason for doing this is to be
able to define functions within macros whose names are composed from
its arguments.

Thanks.

-- Jeff
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.