From: Joel Reymont
Subject: metaclass initialization
Date: 
Message-ID: <1142439298.011589.27080@v46g2000cwv.googlegroups.com>
I'm expecting to see printout from metaclass initialization but I see
none. What I am missing?

(in-package :cl-user)

(defclass base-foo-class (standard-class)
  ((objname :initform nil :reader objname)
   (blah :initform 1 :allocation :class)
   ))

(defclass foo-class (base-foo-class)
  ((hash :initform (make-hash-table) :reader hash)
   (objname :initform "foo-class!")
   ))

(defclass foo ()
  ()
  (:metaclass foo-class))

(defmethod initialize-instance :after ((self base-foo-class) &key)
  (format t "initializing base-foo-class~%"))

(defmethod initialize-instance :after ((self foo-class) &key)
  (format t "initializing foo-class~%"))

(make-instance 'foo)

From: Pascal Costanza
Subject: Re: metaclass initialization
Date: 
Message-ID: <47qtfiFh2ih8U1@individual.net>
Joel Reymont wrote:
> I'm expecting to see printout from metaclass initialization but I see
> none. What I am missing?
> 
> (in-package :cl-user)
> 
> (defclass base-foo-class (standard-class)
>   ((objname :initform nil :reader objname)
>    (blah :initform 1 :allocation :class)
>    ))
> 
> (defclass foo-class (base-foo-class)
>   ((hash :initform (make-hash-table) :reader hash)
>    (objname :initform "foo-class!")
>    ))
> 
> (defclass foo ()
>   ()
>   (:metaclass foo-class))
> 
> (defmethod initialize-instance :after ((self base-foo-class) &key)
>   (format t "initializing base-foo-class~%"))
> 
> (defmethod initialize-instance :after ((self foo-class) &key)
>   (format t "initializing foo-class~%"))
> 
> (make-instance 'foo)

Do you expect the output caused by defining the class 'foo, or by 
creating an instance thereof?

Your initialize-instance methods are defined on class metaobjects. They 
would be triggered if they were defined _before_ the (defclass foo ...) 
form.

If you want to ensure that instances of 'foo and of other classes that 
are instances of your metaclassse trigger the output, you have to add a 
superclass to each of those classes that you can specialize your 
initialize-instance methods on. You can add that superclass 
automatically based on the metaclass. (If you need more info here, feel 
free to ask, but it would first be good to know what you actually want.)


Pascal

P.S.: Whenever you specialize initialize-instance, consider specializing 
reinitialize-instance as well.

-- 
3rd European Lisp Workshop
July 3-4 - Nantes, France - co-located with ECOOP 2006
http://lisp-ecoop06.bknr.net/
From: Joel Reymont
Subject: Re: metaclass initialization
Date: 
Message-ID: <1142440699.408590.225840@u72g2000cwu.googlegroups.com>
Also, is there a way to find out the metaclass instance corresponding
to a class or instance of an object?
From: Joel Reymont
Subject: Re: metaclass initialization
Date: 
Message-ID: <1142440776.247120.172470@z34g2000cwc.googlegroups.com>
Thanks Pascal! Moving the methods before defclass foo does the trick!
From: Pascal Costanza
Subject: Re: metaclass initialization
Date: 
Message-ID: <47qvsbFh66peU1@individual.net>
Joel Reymont wrote:
> Also, is there a way to find out the metaclass instance corresponding
> to a class or instance of an object?

(class-of (find-class 'some-class))

(class-of (class-of some-instance))


Pascal

-- 
3rd European Lisp Workshop
July 3-4 - Nantes, France - co-located with ECOOP 2006
http://lisp-ecoop06.bknr.net/
From: Joel Reymont
Subject: Re: metaclass initialization
Date: 
Message-ID: <1142443113.428875.120960@j52g2000cwj.googlegroups.com>
That would give me the metaclass, right? Not the metaclass instance.

The reason I was asking about the metaclass _instance_ is that I'd like
to pull some data out of my custom fields, for example.
From: Pascal Costanza
Subject: Re: metaclass initialization
Date: 
Message-ID: <47r25vFgua5eU1@individual.net>
Joel Reymont wrote:
> That would give me the metaclass, right? Not the metaclass instance.
> 
> The reason I was asking about the metaclass _instance_ is that I'd like
> to pull some data out of my custom fields, for example.

Ah, that's even simpler. (find-class 'some-class) returns a class 
metaobject [1], as does (class-of some-instance).

So (slot-value (find-class 'some-class) 'some-slot) gives you what you need.


Pascal


[1] Watch out for the more accurate terminology. ;)

-- 
3rd European Lisp Workshop
July 3-4 - Nantes, France - co-located with ECOOP 2006
http://lisp-ecoop06.bknr.net/
From: Joel Reymont
Subject: Re: metaclass initialization
Date: 
Message-ID: <1142448319.763563.119590@j33g2000cwa.googlegroups.com>
*my foot in my mouth* :-)
From: R. Mattes
Subject: Re: metaclass initialization
Date: 
Message-ID: <pan.2006.03.15.19.19.50.58008@hobbes.mh-freiburg.de>
On Wed, 15 Mar 2006 09:18:33 -0800, Joel Reymont wrote:

> That would give me the metaclass, right? Not the metaclass instance.
> 
> The reason I was asking about the metaclass _instance_ is that I'd like
> to pull some data out of my custom fields, for example.

Aehm, 'class-of :: instance -> class'

 (class-of (class-of instance))

   ^           ^
   |           |
   |           *------ returns metaclass instance [1]
   |
   *------------------ returns class of metaclass instance.


 [1] isn't this what you where looking for?

 Cheers, 

   Ralf Mattes