Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is an initform used?

I'm forming a class for some work on molecular dynamics as follows:

(defclass %atom (particle)
  ((name :initarg :name :initform (error "Every atom in the system must have a name!"))
   (mass :accessor mass :initarg :mass :initform (getmass name))
   (charge :accessor charge :initarg :charge :initform (getcharge name))))

Initially I thought that I could somehow refer to other slots within the class definition with an initform i.e. (getmass name) - but that turns out to be untrue (or does it?!?). Instead, I'm told that initialize-instance would be the place to put all that initialization stuff... fair enough.

The question I have, then, is when is :initform used? What's the idiomatic preference? I've seen it used as above for generating (error "...") code, and also to initialize default arguments when an :initarg is not provided. But both of those could easily fit into initialize-instance and may make more sense there. Is there a particular way :initform is generally used?

like image 647
Shamster Avatar asked Nov 05 '09 19:11

Shamster


1 Answers

Usually one would use :initform to give a slot some default value. With defclass one can't compute initforms simply based on other slots. There are also other complications, so :initform should be used for simple forms.

Examples

  • set the z slot of a 3d-vector object to 0.0

  • set the list of windows in a screen object to NIL

  • set the 'key function' slot in an object to #'identity

  • set the 'report string' slot in an error to "an error occured"

Any other more complicated set up of slot values should be done in a method. If the object needs to be set up automatically after MAKE-INSTANCE use an :AFTER method to INITIALIZE-INSTANCE.

like image 146
Rainer Joswig Avatar answered Sep 18 '22 23:09

Rainer Joswig