[Types-sig] Q: What are Meta-classes anyhoo?

Donald Beaudry Donald Beaudry <donb@tensilica.com>
Fri, 04 Dec 1998 13:44:01 -0500


"Evan Simpson" <evan@tokenexchange.com> wrote,
> >Further on, it goes on to say,
> >"Whenever a new class is created, a new metaclass is created for it
> >automatically.  Metaclasses are similar to other classes because they
> >contain methods used by their instances.  Metaclasses are different
> >from other classes because they themselves are not instances of
> >metaclasses.  Instead, they are all instances of a class called
> >Metaclass."
> >
> >So, like Evan's #6, metaclasses contain class methods.  In actuality,
> >in Smalltalk, constuctors are just class methods, ie, methods of
> >class's metaclass.
> 
> Eeek.  I like the part about class methods, but the business of
> automatically generating a new metaclass behind the scenes for every
> new class is a bit odd.

I thought so too, so when I implemented the metaclases for my
objectmodule I didnt bother to make a new metaclass if no changes were
specified... that is, a new metaclass isnt needed if the metaclass
associated with the base class will do.

> If all metaclasses are instances of Metaclass, and you can subclass
> Metaclass (say with MyMetaClass), how do you get an instance of
> MyMetaClass to be the metaclass of a new class?

I am not exactly sure, but..

> Or does Smalltalk let you implicitly define your metaclass in the
> class definition somehow?

Yes.  When defining a class in Smalltalk you must qualify your methods
and members as either class or instance thingies (I forget the exact
keywords needed).  So, when ever you specify a class method, you are
really added a method to the meta-classs's namespace.  When the method
is invoked "self" is the class, not an instance of the class.

So... when you look at the syntax of how classes and metaclass are
specified, it seems pretty reasonable to force the existance of a
metaclass for each class.  This also means, btw, that each metaclass
has only one instance.  It sounds simple and is almost easy to
explain, even if it is somewhat artificial.

For what I thought to be similar reasons, my objectmodule does not
create a metaclass for each class.  Rather, it only creates one when
methods or members of the metaclass are explicitly specified.  In
other words, with my system you dont get a new metaclass unless you
ask for one.  Asking for one in my system is much less subtle than
how you ask for one in Smalltalk.

	--Don