Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Metaclasses

0 views
Skip to first unread message

Dan Sugalski

unread,
Oct 4, 2004, 11:45:50 AM10/4/04
to perl6-i...@perl.org
Okay, color me officially confused. I'm working on the assumption
that metaclasses are needed, but I don't, as yet, understand them.
So, with this bit of ignorance exposed, could someone point me to a
good explanation of what they are and how they work? Theory's fine
(possibly better than fine :) or if someone's local to me and wants
to sit me down and fill me in, that'd be fine too. (I am, alas,
limited to docs that're on-line. My book budget's blown for the year
or I'd go track down the smalltalk book everyone points to as the
Good Resource for 'em)
--
Dan

--------------------------------------it's like this-------------------
Dan Sugalski even samurai
d...@sidhe.org have teddy bears and even
teddy bears get drunk

Michael Walter

unread,
Oct 4, 2004, 12:09:17 PM10/4/04
to Dan Sugalski, perl6-i...@perl.org

Dan Sugalski

unread,
Oct 4, 2004, 1:24:58 PM10/4/04
to Michael Walter, perl6-i...@perl.org
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.

Sam Ruby

unread,
Oct 4, 2004, 1:41:15 PM10/4/04
to Dan Sugalski, perl6-i...@perl.org
Dan Sugalski wrote:
> 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.

Here's another one:

http://www.onlamp.com/pub/a/python/2003/04/17/metaclasses.html

- Sam Ruby

Aaron Sherman

unread,
Oct 4, 2004, 1:50:34 PM10/4/04
to Dan Sugalski, perl6-i...@perl.org
On Mon, 4 Oct 2004 13:24:58 -0400, Dan Sugalski <d...@sidhe.org> wrote:
> >On Mon, 4 Oct 2004 11:45:50 -0400, Dan Sugalski <d...@sidhe.org> 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
AaronJ...@gmail.com or a...@ajs.com

0 new messages