Lisp HUG Maillist Archive

Need a style advice

Hello,

In the :default-initargs section of a class, I would like to specify a list of mappings between strings and functions.  Something like :

(defclass foo (bar)
  ()
  (:default-initargs
   :bar-mappings '(("hello" #'say-hello)
                   ("world" #'see-the-world))))


Where :bar-mappings would be later processed as:

(defmethod initialize-instance :after ((self bar) &key bar-mappings)
  (loop :for mapping :in bar-mappings
        :do (apply #'bar-register-mapping mapping)))


But that does not work because #'hello-function – being in a quoted list – is parsed as (FUNCTION HELLO-FUNCTION).

Other options for writing the definition of foo:

1. Ugly

(defclass foo (bar)
  ()
  (:default-initargs
   :bar-mappings `(("hello" ,#'say-hello)
                   ("world" ,#'see-the-world))))

2. Ugly too

(defclass foo (bar)
  ()
  (:default-initargs
   :bar-mappings (list (list "hello" #'say-hello)
                       (list "world" #'see-the-world))))


I am sure there is a third solution that I have overlooked.
Please share your ideas!


Best Regards,
Camille

Re: Need a style advice

Joshua Taylor kindly reminded me that it is not necessary to get the actual function objet to funcall it, a symbol also works.  The answer was obviously obvious.  Thank you Joshua!



On 20 janv. 2012, at 15:46, Camille Troillard wrote:

> 
> Hello,
> 
> In the :default-initargs section of a class, I would like to specify a list of mappings between strings and functions.  Something like :
> 
> (defclass foo (bar)
>  ()
>  (:default-initargs
>   :bar-mappings '(("hello" #'say-hello)
>                   ("world" #'see-the-world))))
> 
> 
> Where :bar-mappings would be later processed as:
> 
> (defmethod initialize-instance :after ((self bar) &key bar-mappings)
>  (loop :for mapping :in bar-mappings
>        :do (apply #'bar-register-mapping mapping)))
> 
> 
> But that does not work because #'hello-function – being in a quoted list – is parsed as (FUNCTION HELLO-FUNCTION).
> 
> Other options for writing the definition of foo:
> 
> 1. Ugly
> 
> (defclass foo (bar)
>  ()
>  (:default-initargs
>   :bar-mappings `(("hello" ,#'say-hello)
>                   ("world" ,#'see-the-world))))
> 
> 2. Ugly too
> 
> (defclass foo (bar)
>  ()
>  (:default-initargs
>   :bar-mappings (list (list "hello" #'say-hello)
>                       (list "world" #'see-the-world))))
> 
> 
> I am sure there is a third solution that I have overlooked.
> Please share your ideas!
> 
> 
> Best Regards,
> Camille
> 


Updated at: 2020-12-10 08:37 UTC