Hi Frank.
> I'm fairly new to EIEIO and Emacs but I was wondering how to do this:
>
> Lets say I have this class:
>
> (defclass foo ()
> ((size :initarg :size
> :type wholenum)
> (vec :initform (make-vector size nil)
> :protection :protected)))
>
> Which embed's a vector.
>
> My question is how do I get VEC initialized. This code obviously
> doesn't work. Can I somehow reference the previously initialized
> arguments (in this case SIZE) in an initform?
In EIEIO, initforms are very limited, unlike "proper" CLOS. They are not
evaluated and if they where, the environment in which they are defined
would not be available.
> Is it possible to write my own constructor which executes before the
> default constructor like this?
>
> (defmethod foo :before :static ((obj foo) &rest args)
> )
As you assume, writing a constructor can solve the problem. However, the
constructor method is called initialize-instance.
The following code fragment demonstrates two ways in which the
constructor method could be written:
(defclass foo ()
((size :initarg :size
:type wholenum)
(vec :initarg :vec
:protection :protected)))
(defmethod initialize-instance ((this foo) slots)
"Initialize slots of THIS."
(call-next-method) ;; Calls initialize-instance of default
;; superclass to allocate the instance and
;; initialize slots (size here)
(with-slots (size vec) this
(setq vec (make-vector size nil))))
;; OR
(defmethod initialize-instance :after ((this foo) slots)
"Set value of :vec slot of THIS after initialization."
;; THIS is already initialized in this case
(with-slots (size vec) this
(setq vec (make-vector size nil))))
(foo "foo" :size 10)
(make-instance 'foo :size 10)
Hope that helps.
Kind regards,
Jan
|