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/
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/
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.
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/
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