Subject: Re: Idiom to call function from string
From: rpw3@rpw3.org (Rob Warnock)
Date: Sun, 09 Jul 2006 20:31:07 -0500
Newsgroups: comp.lang.lisp
Message-ID: <1rOdncIuiKBGMCzZnZ2dnUVZ_vOdnZ2d@speakeasy.net>
I just wrote:
+---------------
| Sascha Wilde  <wilde@sha-bang.de> wrote:
| +---------------
| | I want to call an existing function after construction it's name from
| | strings.  I came up with:
| | (funcall (find-symbol (string-upcase (concatenate 'string 
| |                                                   "foo-" "bar")) 
| |                       :baz))
...
|     (defmacro def-foo-function (name &body body)
|       `(progn (defun ,name () ,@body)
| 	      (setf (gethash ,(string-downcase (symbol-name name))
| 			     *foo-functions*)
|                     #',name)))
+---------------

Oops! Sorry, I forgot that prefixing the function names with "FOO-"
was part of your spec. Please replace the above DEF-FOO-FUNCTION
with this one:

     (defmacro def-foo-function (name &body body)
       (let ((mangled-name (intern (concatenate 'string
						(symbol-name :foo-)
						(symbol-name name)))))
	 `(progn (defun ,mangled-name () ,@body)
		 (setf (gethash ,(string-downcase (symbol-name name))
				*foo-functions*)
			#',mangled-name))))

[Note that the name-mangling happens only at DEF-FOO-FUNCTION time,
*not* at runtime...]

The rest of the code remains the same as before. [Well, except that
to *completely* conform to your spec, the whole file should start
with (IN-PACKAGE :BAZ).]


-Rob

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607