On Mon, 4 Oct 2004 13:24:58 -0400, Dan Sugalski <[EMAIL PROTECTED]> wrote:
> >On Mon, 4 Oct 2004 11:45:50 -0400, Dan Sugalski <[EMAIL PROTECTED]> wrote:
> >>  Okay, color me officially confused. I'm working on the assumption
> >>  that metaclasses are needed, but I don't, as yet, understand them.

> At 12:09 PM -0400 10/4/04, Michael Walter wrote:
> >http://members.rogers.com/mcfletch/programming/metaclasses.pdf

> I do have that one. Unfortunately it's the PDF of slides and, while
> it looks like if I was at the talk it'd all make sense, without the
> talk that goes with 'em... not so much sense.

A metaclass is simply an object which represents the class itself and
can perform operations on the class. One (IMHO bad) reason to do this
is for aspect oriented programming, AKA "making object oriented
programming even harder to debug". This is where you modify a class
on-the-fly to inject behaviors or conditions before or after events
(usually method invocations, specfically).

Metaclasses can also be used to do things like sub-class on the fly
(e.g. mix-ins). When you do this, you invoke a method on the metaclass
which requests a new class (a sort of copy constructor for classes)
with a new set of behaviors (usually via an inheritance mechanism).

Perl 6, for example, will be able to say:

  my Dog $spot .= new;
  my Dog $greyhound := $spot but Animal::Fast;

or something that looks strikingly like that. In this case, $greyhound
isn't really a Dog, it's an anonymous class type which was generated
by the "but" and instantiated from $spot.

More importantly, you could do the same thing when you say:

  my Dog $spot .= new;
  my Dog $dogbiscut .= new;
  $dogbiscut.race($spot but Animal::Fast);

Here, the Dog.race method takes, we presume, a Dog, but we're passing
it something that has an additional role attached (Animal::Fast).
Everything still works, but the role might change how this particular
dog works in ways that the original Dog designer didn't have in mind.

IMHO this is the correct reason (though there are other reasons,
mostly dealing with introspection and debugging) to want metaclasses.
Mixins for Python, Ruby and Perl 6 become trivial given this
mechanism. Of course, I'm not SURE you need to put this in Parrot...
it really depends on how valuable it is to be able to do it the same
way in all compilers.

If you want to embrace this in Parrot, you're probably going to want
to use something akin to the Perl 6 model, since it's designed to be
able to emulate the Python, Ruby and Scheme models.

-- 
Aaron Sherman
Senior Systems Engineer and Toolsmith
[EMAIL PROTECTED] or [EMAIL PROTECTED]

Reply via email to