Consider the following (incomplete, non-working) definition:
(defun foo (subrequest &rest args)
(labels ((f1 () "Hello")
(f2 () "World"))
(apply-label subrequest args)))
What I'd *like* to be able to do is:
(foo 'f1) => "Hello"
(foo 'f2) => "World"
However, I can't figure out how to define #'apply-label to get
this behavior. #'symbol-function seems only to deal with top-
level definitions, and #'function of course acts like #'quote
and so doesn't evaluate its argument...
-- Kaelin
In article <··················@newsin1.ispchannel.com>, "Kaelin
Colclasure" <······@everest.com> wrote:
>
> (defun foo (subrequest &rest args)
> (labels ((f1 () "Hello")
> (f2 () "World"))
> (apply-label subrequest args)))
>
> What I'd *like* to be able to do is:
>
> (foo 'f1) => "Hello"
> (foo 'f2) => "World"
Perhaps there is a better organization of your code that avoids the
function lookup?
(defmacro applicable-labels ((&rest fdefs) &body body)
(let ((cases (loop for x in fdefs
collect (let ((name (car x)))
`(,name #',name)))))
`(labels (,@fdefs
(apply-label (name args)
(apply
(ecase name
,@cases)
args)))
. ,body)))
(defun foo (subrequest &rest args)
(applicable-labels ((f1 () "Hello")
(f2 () "World"))
(apply-label subrequest args)))
What I'd *like* to be able to do is:
(foo 'f1) => "Hello"
(foo 'f2) => "World"
David D. Smith <···@flavors.com> wrote in message
·························@p050.bit-net.com...
[...]
> Perhaps there is a better organization of your code that avoids the
> function lookup?
[...]
Thanks for the solution, David! The example was, of course, contrived
to outline my goal. In my application, the lexical scope provided by
the function wrapping the labels provides a lot of context. And I
can't use special variables in this case because of other constraints.
After I posted my question, it eventually occurred to me why what I
was initially expecting to work did not. Once a function is compiled,
there's really no reason to keep any association with the symbol a
label was "bound" to around in the generated code. It makes perfect
sense that if I want to retain that association I need to do it
explicitly. [I love it when a language makes perfect sense.]
-- Kaelin