[Openmcl-devel] CCL <-> C++ interface generator in alpha, reviewers wanted
dherring at tentpost.com
dherring at tentpost.com
Fri Apr 8 08:53:09 PDT 2011
Jon Anthony wrote:
> Jason E. Aten wrote:
> ;; Since Common Lisp doesn't support method overloading (apparently,
> ;; please correct me if I've misunderstood the comments at the bottom of
> ;; page 194 of Practical Common Lisp),
>
> See the lambda list keywords: optional, rest and key. For this simple
> example, optional looks like what you want.
In CLOS, generic functions distinguish between required and optional
keywords; dispatch only occurs on the required keywords. (I feel I've
misstated the details, but the gist is ok.) Conceptually, all CLOS
dispatch happens at runtime, like virtual functions in C++. (CLOS
implementations may leverage compile-time constants and type information
to achieve static dispatch.)
C++ static dispatch follows a different ruleset; it can distinguish
between both the arity and type of arguments. For virtual functions, C++
runtime dispatch invokes a function implementation stored in the vtable of
the first argument (left of the dot).
Here's one approach for expressing C++ semantics in CL. (Warning: Code
typed over lunch and not tested.)
// C++
struct S
{
virtual void f(int);
virtual void f(float);
virtual void f(int, float);
...
};
;; CL
(defstruct S ...)
(defgeneric f_1 (s x)
(:method ((s S) (x int)) ...)
(:method ((s S) (x float)) ...))
(defgeneric f_2 (s x y)
(:method ((s S) (x int) (y float)) ...))
(declaim (inline f))
(defun f (&rest args)
"arity dispatch for S::f in C++"
(ecase (length args)
(1 (funcall f_1 args))
(2 (funcall f_2 args))))
Later,
Daniel
More information about the Openmcl-devel
mailing list