[Openmcl-devel] CCL <-> C++ interface generator in alpha, reviewers wanted

Ron Garret ron at flownet.com
Mon Apr 11 07:37:07 PDT 2011


On Apr 10, 2011, at 9:10 PM, Jason E. Aten wrote:

> Is it possible to overload constructors in CLOS? (So that one ctor takes zero arguments, and another takes two args, for example).

Sort of.

You can't "overload" a generic function in the same way that you can overload a function in C++.  All methods of a generic function have to have congruent lambda lists (see the hyperspec section 7.6.4.)

But because you're dealing with keyword arguments, they are already optional.  So you can do your own dispatching:

(cl:defmethod initialize-instance :after ((obj Vlad) &key (a cl:integer) (d cl:number))
  (cond ((and a b)
              (setf (slot-value obj 'ff-pointer) (new_Vlad__SWIG_0 a d))))
             ((not (or a b))
              (setf (slot-value obj 'ff-pointer) (new_Vlad__SWIG_1))))
             (a (error "A specified but B isn't"))
             (b (error "B specified but A isn't")))))

or something like that.  BTW:

> ; this works:
> (cl:defmethod initialize-instance :after ((obj Vlad) &key (a cl:integer) (d cl:number))
>   (setf (slot-value obj 'ff-pointer) (new_Vlad__SWIG_0 a d)))

This doesn't do what you think it does.  You think this specifies A and D to have types of cl:integer and cl:number, but it doesn't.  It specifies A and D to have default values of cl:integer and cl:number, which are unbound variables.  Try this:

(cl:defmethod initialize-instance :after ((obj Vlad) &key (a cl:integer) (d cl:number))
  (setf (slot-value obj 'ff-pointer) (new_Vlad__SWIG_0 a d)))

(setf o2 (make-instance 'Vlad))

and you'll see what I mean.

rg




More information about the Openmcl-devel mailing list