[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Changing the :INITFORM of a slot with :ALLOCATION :CLASS



    Date: Wed, 13 Mar 1991 10:13 EST
    From: lgm@iexist.att.com

    My CLOS classes often have associated with them various parameters which
    are not per-instance but merely per-class.  In Flavors I simply used
    DEFVAR or DEFPARAMETER to define such a variable or parameter, but with
    CLOS I would prefer to tie the variable/parameter more tightly to its
    class by defining it as a slot with :ALLOCATION :CLASS.  My difficulty
    is that once such a slot has been defined and initialized via :INITFORM,
    the slot's value is frozen forever (unless explicitly modified via an
    ad-hoc SETF).  That is, simple re-evaluation of the class definition
    with a different :INITFORM does not alter the slot's value.  Only if I
    remove the slot (or change its allocation mode), then put it back again,
    is the slot reinitialized.  My understanding of the CLOS spec is that
    this is indeed the required behavior.  But isn't there any way to get
    the behavior 1I0 want (modification of the slot's value when redefined
    with a different :INITFORM)?  Or is this simply the wrong application of
    :ALLOCATION :CLASS?

    Note that Common Lisp specifies for DEFVAR the same style of behavior.
    That is, re-evaluation of a DEFVAR with a new initializer is not
    supposed to modify the variable's value, but Symbolics' implementation
    does so anyway (and thankfully so, in my opinion), albeit with a
    warning.


	    Lawrence G. Mayka
	    AT&T Bell Laboratories
	    lgm@iexist.att.com

    Standard disclaimer.

My feeling has been that shared slots are not an appropriate place to
store information about a class.  Slots in general are for attributes of
instances.  If it happens that all the instances of a class have the
same value associated with that attribute, then a shared slot is
appropriate.  An example is the weight of some part in a mechanical CAD
program.  All parts of a particular kind have the same weight, but the
weight is not a property of the class; it is a property of the part.

All this just makes your real question more pressing: what is the right
implementation for properties of a class?

I keep hoping that X3J13 will accept enough of the meta-object protocol
to allow this to work:

(defclass counting-class (standard-class)
   ((number-of-instances :initform 0 :accessor number-of-instances)))

(defmethod make-instance :after ((class counting-class) &key)
   (incf (number-of-instances class)))

(defclass elephant ()
   ((size :initform 'big)
    color
    name)
  (:metaclass counting-class))

(number-of-instances (find-class 'elephant)) => 0

(make-instance 'elephant)

(number-of-instances (find-class 'elephant)) => 1