[Openmcl-devel] Trouble defining ObjC classes with struct slots

Daniel Dickison danieldickison at gmail.com
Wed May 23 13:41:32 PDT 2007

I'm trying to create an Objective-C class in OpenMCL via the bridge  
which has a couple of NSPoint slots, and I'm running into a couple of  
problems.  I'm not sure if the following behaviors are intended, but  
they sure stumped me for a while!

1. If I define the slot to have a :foreign-type of :<NSP>oint, then  
when I access the slot I get a generic foreign pointer which is not  
recognized as an :<NSP>oint.  Example:

	(defclass a-point (ns:ns-object)
	  ((point :foreign-type :<NSP>oint))
	  (:metaclass ns:+ns-object))

	(setf p (make-instance 'a-point))
	(ns:ns-point-p (slot-value p 'point))
	==> nil

This means I can't use functions like ns:ns-point-x with the slot,  
which in inconvenient but work-aroundable using (pref (slot-value p  
'point) :<NSP>oint.x).  Is there a way to convince the runtime that  
these foreign slots are in fact NSPoints?

2. If I define the slot to be a CLOS slot (i.e. no :foreign-type  
option), then I can store NSPoints in the slot and get back NSPoints  
as expected, BUT, any initialization I do during the ObjC -init  
method gets overridden by the :initform.  It seems that CLOS slots  
get re-initialized after the ObjC -init method runs, so the ObjC  
initializations get wiped out.  Example:

	(defclass b-point (ns:ns-object)
	  ((point :initform (ns:make-ns-point 1.0 2.0)))
	  (:metaclass ns:+ns-object))
	(objc:defmethod (#/init :id) ((self b-point))
	  (format t "Before: ~A   " (slot-value self 'point))
	  (setf (slot-value self 'point) (ns:make-ns-point 3.0 4.0))
	  (format t "After: ~A" (slot-value self 'point))
	(setf p (make-instance 'b-point))   ;; Before: #<NS-POINT 1,2...>    
After: #<NS-POINT 3,4...>
	(slot-value p 'point)
	==> #<NS-POINT 1,2...>

This as also work-aroundable by doing the CLOS slot initialization in  
an initialize-instance :after method, but it seems wrong to do the  
same initialization in 2 different places depending on whether Lisp  
or Objective-C created the instance.


More information about the Openmcl-devel mailing list