Subject: creating metaclass

From: Jean-Christophe Groult <[email-masked]>
Date: Fri, 14 May 1999 00:10:49 +0200 (MET DST)

Hello,

I am student and I work on a projet written in CLOS.
I need some class which should have some slots which are themselves
instances of class. The trouble is to make instances of slots
*automatically*. 
I believe metaclass could solve this.
What I want to do, is something like below :

(defclass my-metaclass (standard-class)
 ( ... maybe some slots here ...)
)
... some method here. Probably initialize-instance :around (or :after)

(defclass slots-classA (...) (...) )
... some method ...

(defclass slots-classB (...) (...) )
... some method ...

(defclass first-class (some-superclass)
  ((slotA  :type slots-classA)
   (slotC  :initarg :C))
  (:metaclass my-metaclass))
... some method ...

(defclass second-class (some-superclass)
  ((slotA  :type slots-classA)
   (slotB  :type slots-classB)
   (other-slotB   :type slot-classB))
  (:metaclass my-metaclass)
)
... some method ...

;and then

(setf foo (make-instance 'first-class))
 => automatic call of (setf slotA (make-instance 'slots-classA))
    nothing for slotC

(setf bar (make-instance 'second-class))
 => automatic call of (setf slotA (make-instance 'slots-classA))
 => automatic call of (setf slotB (make-instance 'slots-classB))
 => automatic call of (setf other-slotB (make-instance 'slots-classB))

So, I have few questions now ;-)

How to create a metaclass ! ?
I have some examples in a (bit old) book. But they don't work, even this
simple one :

[1]> (defclass foo (standard-class) () )

*** - DEFCLASS FOO: superclass #<STRUCTURE-CLASS STANDARD-CLASS> should
belong to class STANDARD-CLASS
1. Break [2]> 

standard-class is not the goog one, so which one should I use ?

And how can I get the value associated whith the :type keyword ?
Can I have some trouble using an already defined keyword ?
Is it better to use a new one ?

If someone have experience with metaclass, or some working examples:
please, help ! :-)

thanks for having read all,

JC

Jean-Christophe GROULT
mail : [email-masked]
DEA informatique                                  Universite de CAEN
Éviter le déshonneur est un fait indépendant de la victoire ou de la défaite

---

From: Bruno Haible <[email-masked]>
Date: Fri, 14 May 1999 14:33:23 +0200 (MET DST)

Jean-Christophe Groult <[email-masked]> writes:

> 1. Break [2]>=20

In clisp, since the superclass `standard-class' is of metaclass
structure-class, you also need to make `foo' of metaclass structure-class:

  (defclass foo (standard-class) () #+clisp (:metaclass structure-class))

> If someone have experience with metaclass, or some working examples:

The following sample is contained in clisp's testsuite (and was taken
from closette's testsuite):

(defclass rectangle2 ()
  ((x :initform 0.0 :initarg x) (y :initform 0.0 :initarg y)))
(defclass counted2-class (standard-class)
  ((counter :initform 0)) #+CLISP (:metaclass structure-class))
(defclass counted2-rectangle (rectangle2) () (:metaclass counted2-class))
(defmethod make-instance :after ((c counted2-class) &rest args)
  (incf (slot-value c 'counter)))
(slot-value (find-class 'counted2-rectangle) 'counter)
(make-instance 'counted2-rectangle)
(slot-value (find-class 'counted2-rectangle) 'counter)

Bruno