[Bug-openmcl] DEFCLASS and the MOP

Gary Byers gb at clozure.com
Mon Jan 12 22:40:40 MST 2004


One of the tests in the GCL ansi-tests suite does something like:

(progn
  (setf (find-class 'foo) nil)
  (let* ((a (eval '(defclass foo () ())))
         (b (make-instance 'foo))
         (c (progn (setf (class-name a) nil)
	           (eval '(defclass foo () ())))))
    ...))

and checks that A and C are distinct (i.e., that the second DEFCLASS
created a new class rather than reinitialized the old one.)

A class C has the proper name S if (CLASS-NAME C) is S and (FIND-CLASS S)
returns C.  Changing the name of the class a above causes it to cease
to be "properly named".

CLHS says (of DEFCLASS) that "... if a class with the same proper name
exists, it is redefined"; in all other contexts, it describes DEFCLASS
as being a mechanism for the creation of new classes.

The MOP function ENSURE-CLASS calls an ENSURE-CLASS-USING-CLASS method
specialized to the class of what (FIND-CLASS name nil) returned (without
regard for whether this name is a "proper" name.)

Implementing DEFCLASS purely in terms of ENSURE-CLASS seems preferable;
an alternative might be something like:

(defun ensure-class-for-defclass (name &rest keys &key &allow-other-keys)
  (let* ((existing-class (find-class name nil)))
    (when (and existing-class (not (eq (class-name existing-class) name)))
      ;; Class isn't properly named; act like it didn't exist
      (setq existing-class nil))
    (apply #'ensure-class-using-class existing-class name keys)))

Or maybe:

(defparameter CCL:*defclass-redefines-improperly-named-classes-pedantically*
   t)

(defun ensure-class-for-defclass (name &rest keys &key &allow-other-keys)
  (let* ((existing-class (find-class name nil)))
    (when (and CCL:*defclass-redefines-improperly-named-classes-pedantically*
               existing-class
              (not (eq (class-name existing-class) name)))
      ;; Class isn't properly named; act like it didn't exist
      (setq existing-class nil))
    (apply #'ensure-class-using-class existing-class name keys)))

Some people (besides test suite authors, who pretty much have to do
so) might prefer or expect the CLHS behavior.  This is a fairly
obscure case (and doesn't affect what ENSURE-CLASS does, for people
who want to use it directly.)

1 bug down, 400-some-odd to go ...






More information about the Bug-openmcl mailing list