Re: symbol-function vs defun question

Christophe Rhodes <[email protected]>
Newsgroups gmane.lisp.steel-bank.general
Message-ID <[email protected]>
Jeff Cunningham <[email protected]> writes:

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

Can you clarify this?

The "normal" way I would imagine of using macros to define functions
with generated names is to expand into a defun.  Something like:

(defmacro deffoo (suffix)
  `(defun ,(intern (concatenate 'string "FOO-" suffix)) (&rest args)
      ...))

and in this case there's no difficulty to define functions within macros
whos named are composed from arguments.

If you're talking about actually evaluating the definition during the
expansion of the macro, my advice would be: don't do that if you can
possibly avoid it.  (Why?  Two reasons: one, it's awkward, and two: the
macroexpansion environment doesn't necessarily persist).

If you really need the definition to be available at compile-time for
processing subsequent forms in the same file, I would still suggest
expanding into a `(defun ...) form, but in an eval-when:

(defmacro deffoo-always (suffix)
  `(eval-when (:compile-toplevel :load-toplevel :execute)
     (defun ,(intern ...) (&rest args)
        ...)))

though that does mean that this only has the "always" effect if the
deffoo-always is at toplevel.

If none of this works, you can inform the compiler that a function of
the name being defined exists, by expanding into

  `(declaim (ftype function ,(intern ...)))

or if you need to evaluate at macroexpansion-time

  (proclaim `(ftype function ,(intern ...)))

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