Lisp HUG Maillist Archive

dspec - finding source for method

hi list,

i am lisp-newbie, and i already have a question ;-)

i want to use simpler names for capi methods (e.g.
instead of (option-pane-enabled x) or (button-enabled x)
i want to write (enabled x) and have the generic
method machinery worry about the details.

(eval-when (:compile-toplevel :load-toplevel :execute)

   (defun capi-fname (fn cls)
     "computes the capi function name CLS-FN"
     (find-symbol
      (concatenate 'string
                   (symbol-name cls)
                   "-"
                   (symbol-name fn))))

   (defun capi-reader-definition (fn cls)
     "compute defmethod for capi reader"
     `(defmethod ,fn ((w ,cls)) (,(capi-fname fn cls) w)))

   (defun capi-setf-definition (fn cls)
     "compute defmethod for capi setf definition"
     `(defmethod (setf ,fn) (val (w ,cls))
        (setf (,(capi-fname fn cls) w) val))))

(defmacro def-capi-accessors (fname (&rest classes))
   "define accessors for capi classes"
   `(progn ,@(mapcan (lambda (cls)
                       (list
                        (capi-reader-definition fname cls)
                        (capi-setf-definition fname cls)))
                     classes)))

(def-capi-accessors enabled (button simple-pane option-pane))

so far so good, this seems to work - at least for "enabled"
and the few other methods i wrapped so far.

but i would also like to "Find Source" on these generated
methods and be transported to the corresponding def-capi-accessors
line in the editor.

so far, no joy.

for another macro

(defmacro deftest (name &args args) ... )
(dspec:define-dspec-alias deftest (name)
               `(defun ,name))

works like a charm, but then that's easy.

now i suppose i have to tell lispworks something like

(dspec:define-dspec-alias def-capi-accessors (name &rest args)
   (mapcar (lambda (x)
     `(defmethod ,(capi-fname 'enabled x))) ,args)

except it does not work.  i suspect that
  - the expansion is not correct
  - i have to to use an entirely different macro/function ...


how do i do this?

regards,
juergen


Re: dspec - finding source for method

Hi Juergen,

For a lisp newbie, this is already quite a macro. ;-)

I don't know enough about dspecs, so I cannot answer your question 
directly. However, my recommendation would be to drop the macro you 
have written, for the following reason.

Your macro seems to work well for the simple cases that you seem to 
have in mind. However, it is likely that at some later stage you would 
like to add more features. For example, you may want to reorder 
original arguments, add optional arguments, treat keyword arguments in 
certain ways, take before/after/around methods into account, and so on. 
This is likely to become very complex. On the other hand, Common Lisp 
already provides a way to write method definitions in a somewhat more 
concise way. For your example you could write this:

(defgeneric enabled (thing)
   (:method ((thing button))
    (button-enabled thing))
   (:method ((thing simple-pane))
    (simple-pane-enabled thing))
   (:method ((thing option-pane))
    (option-pane-enabled thing)))

In the beginning when I was new to Common Lisp, I have thought that 
"default" methods in defgeneric forms are somewhat superfluous, but in 
the meantime I tend to use them on a regular basis for similar reasons 
that you seem to have in mind. It's a little bit more to type than your 
def-capi-accesors but your def-capi-accessors is likely to become as 
large in the long run.

Just my 0.02€.


Pascal

On 6 Mar 2005, at 2:53, Juergen Gmeiner wrote:

> hi list,
>
> i am lisp-newbie, and i already have a question ;-)
>
> i want to use simpler names for capi methods (e.g.
> instead of (option-pane-enabled x) or (button-enabled x)
> i want to write (enabled x) and have the generic
> method machinery worry about the details.
>
> (eval-when (:compile-toplevel :load-toplevel :execute)
>
>   (defun capi-fname (fn cls)
>     "computes the capi function name CLS-FN"
>     (find-symbol
>      (concatenate 'string
>                   (symbol-name cls)
>                   "-"
>                   (symbol-name fn))))
>
>   (defun capi-reader-definition (fn cls)
>     "compute defmethod for capi reader"
>     `(defmethod ,fn ((w ,cls)) (,(capi-fname fn cls) w)))
>
>   (defun capi-setf-definition (fn cls)
>     "compute defmethod for capi setf definition"
>     `(defmethod (setf ,fn) (val (w ,cls))
>        (setf (,(capi-fname fn cls) w) val))))
>
> (defmacro def-capi-accessors (fname (&rest classes))
>   "define accessors for capi classes"
>   `(progn ,@(mapcan (lambda (cls)
>                       (list
>                        (capi-reader-definition fname cls)
>                        (capi-setf-definition fname cls)))
>                     classes)))
>
> (def-capi-accessors enabled (button simple-pane option-pane))
>
> so far so good, this seems to work - at least for "enabled"
> and the few other methods i wrapped so far.
>
> but i would also like to "Find Source" on these generated
> methods and be transported to the corresponding def-capi-accessors
> line in the editor.
>
> so far, no joy.
>
> for another macro
>
> (defmacro deftest (name &args args) ... )
> (dspec:define-dspec-alias deftest (name)
>               `(defun ,name))
>
> works like a charm, but then that's easy.
>
> now i suppose i have to tell lispworks something like
>
> (dspec:define-dspec-alias def-capi-accessors (name &rest args)
>   (mapcar (lambda (x)
>     `(defmethod ,(capi-fname 'enabled x))) ,args)
>
> except it does not work.  i suspect that
>  - the expansion is not correct
>  - i have to to use an entirely different macro/function ...
>
>
> how do i do this?
>
> regards,
> juergen
>
>
--
So the essence of XML is this: the problem it solves is not hard, and 
it does not solve the problem well. - Jerome Simeon and Philip Wadler



Re: dspec - finding source for method

Juergen Gmeiner wrote:

 > (dspec:define-dspec-alias def-capi-accessors (name &rest args)
 >   (mapcar (lambda (x)
 >     `(defmethod ,(capi-fname 'enabled x))) ,args)

by a combination of reading through the list archives and trial & error,
i got find-source to work.

(defmacro def-capi-accessors (fname (&rest classes))
   `(dspec:def (def-capi-accessors ,fname)
      (progn ,@(mapcan (lambda (cls)
                         (list
                          (capi-reader-definition fname cls)
                          (capi-setf-definition fname cls)))
                       classes))))
(dspec:define-form-parser def-capi-accessors (name)
   `(,def-capi-accessors ,name))

lots of interesting stuff in the archives btw

cheers,
juergen


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