From: David Hanley
Subject: CLOS question
Date: 
Message-ID: <38075E7C.104433AB@ncgr.org>
Let's say that I have a bunch of normal lisp functions
that operate on several structures.  I'd like to make them
accessable via CLOS, but without turning them into
normal CLOS functions, and without lots of code
duplication.  Ideally, I'd like to tell CLOS:
"Take the function, and add it to this method table, with
this type specialization."  Here's a way to
solve the problem, but with a inefficent extra call:


(defun foo-add-int( foo x )
    "add an int to foo" (...))

(defun bar-add-int( bar x )
    "add an int to bar" (...))

;;
;; clos bindings
;;

(defgeneric add-int( cont val ) )

(defmethod ( (f foo) x )
    (foo-add-int f x))

(defmethod ( (b bar) x)
    (bar-add-int f x))

However, knowing how CLOS works, it seems
like there should be a way to pop my function right into
the dispatch table, and avoid the extra cost of passing
the call through a dummy CLOS method.

My goal is to use the CLOS bindings in high-level code,
but to revert to normal function calls( proabably inlined )
in tight time-critical loops.

Thanks for any assistance!

dave

From: Barry Margolin
Subject: Re: CLOS question
Date: 
Message-ID: <FvJN3.911$854.37982@burlma1-snr2>
In article <·················@ncgr.org>, David Hanley  <···@ncgr.org> wrote:
>However, knowing how CLOS works, it seems
>like there should be a way to pop my function right into
>the dispatch table, and avoid the extra cost of passing
>the call through a dummy CLOS method.

I don't have a copy of the MOP handy, but I expect it has a way to create a
method object whose body is a supplied ordinary function (this is what
defmethod has to expand into).

>My goal is to use the CLOS bindings in high-level code,
>but to revert to normal function calls( proabably inlined )
>in tight time-critical loops.

If the functions are defined as inline, then they should be inlined into
the defmethods as well, so there's no performance impact of doing it with
explicit wrappers.  You could define a simple macro that defines the inline
function and the wrapper method all at once, something like this (a very
minimal implementation -- it doesn't handle some &-keywords, multiple
dispatch, etc.):

(defmacro define-inline-method (name args &body body)
  (let ((arg-names (mapcar #'(lambda (arg)
                               (if (listp arg) (car arg)
                                   arg))
                           args))
        ;; Internal function name is <name>-<first-arg-class>
	(function-name (intern (format "~s-~s" name (second (car args))))))
    `(progn
       (declaim (inline ,function-name))
       (defun ,function-name ,arg-names
         ,@body)
       (defmethod ,name ,args
         (,function-name ,@arg-names)))))

-- 
Barry Margolin, ······@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: Fernando D. Mato Mira
Subject: Re: CLOS question
Date: 
Message-ID: <380AEB20.42213F16@iname.com>
--------------3446BE014EA1CC3FA9DA386C
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

"H. Tunc Simsek" wrote:

> (eval `(defmethod add (a b) (funcall #'+ a b)))

     ^^^^^^^^^^^^^^

Yuck.

--

Fernando D. Mato Mira
Real-Time SW Eng & Networking
Advanced Systems Engineering Division
CSEM
Jaquet-Droz 1                   email: matomira AT acm DOT org
CH-2007 Neuchatel                 tel:       +41 (32) 720-5157
Switzerland                       FAX:       +41 (32) 720-5720

www.csem.ch      www.vrai.com     ligwww.epfl.ch/matomira.html



--------------3446BE014EA1CC3FA9DA386C
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
"H. Tunc Simsek" wrote:
<blockquote TYPE=CITE>(eval `(defmethod add (a b) (funcall #'+ a b)))</blockquote>
&nbsp;&nbsp;&nbsp;&nbsp; ^^^^^^^^^^^^^^
<p>Yuck.
<pre>--&nbsp;


Fernando D. Mato Mira&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Real-Time SW Eng &amp; Networking&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Advanced Systems Engineering Division
CSEM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Jaquet-Droz 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; email: matomira AT acm DOT org
CH-2007 Neuchatel&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tel:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; +41 (32) 720-5157
Switzerland&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FAX:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; +41 (32) 720-5720

www.csem.ch&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; www.vrai.com&nbsp;&nbsp;&nbsp;&nbsp; ligwww.epfl.ch/matomira.html</pre>
&nbsp;</html>

--------------3446BE014EA1CC3FA9DA386C--