I promise I will learn hwo to use my email program. This should have gone to the mailing list :-/

Dan Sugalski wrote:

At 10:22 PM +0000 3/12/04, Mark Sparshatt wrote:

Hi,

I've been reading PDD15. It seems that if the object foo is an instance of the class Foo then foo is a ParrotObject pmc and Foo is a ParrotClass pmc.

From the description in PDD15 I'm not sure how to hand languages where a class is also an object. Where Foo is an instance of Foo' which is an instance of Class.


Okay, I'm going to be dense here for a bit, as I just don't do objects. (Well, except grudgingly, and with an inordinate amount of grumbling) So the big question is... What Does This Mean? I'm reasonably sure (though not 100% sure) that classes can only inherit from classes, not objects, so there's none of this template object stuff to deal with. The ParrotObject PMC inherits from the ParrotClass PMC, so there's a lot of overlap, so... what can one do with a class that makes it a metaclass? (Yeah, I do realize I'm more or less asking a "Recap the last 30 years of OO theory for Dummies" question, but I'm not sure I'm familiar enough with things to ask a better one)

I'm not 100% certain about the details but I think this is how it works.


In languages like C++ objects and classes are completely seperate.
classes form an inheritance heirachy and objects are instances of a
particular class.

However in some languages (I think that Smalltalk was the first) there's
the idea that everything is an object, including classes. So while an
object is an instance of a class, that class is an instance of another
class, which is called the metaclass. I don't there's anything special
about these classes other than the fact that their instances are also
classes.


Thinking about it I think you may have the relationship between ParrotObject and ParrotClass the wrong way around. Since a class is an object but and object isn't a class it would be better for ParrotClass to inherit from ParrotObject, rather than the other way round.

In Ruby when you create a class Foo, the Ruby interpreter automatically
creates a class Foo' and sets the klass attribute of Foo to point to Foo'.

This is important since class methods of Foo are actually instance
methods of Foo'. Which means that method dispatch is the same whether
you are calling an instance of class method.

foo.method()

looks at foo's klass attribute then checks the returned class object
(Foo) for method

Foo.method()

looks at Foo's klass attribute and again checks the returned class
object (Foo') for method.

The Pickaxe book has got a better explanation of this (at
http://www.rubycentral.com/book/classes.html though without any diagrams
:( )

In Python when defining a class it's possible to set an attribute in the
class that points to the classes metaclass. The metaclass itself is just
a normal class that defines methods which override the normal behaviour
of the class.

IIRC Python has got both class methods and meta class instance methods
which work almost (but not quite) in the same way as each other.

Hopefully someone with more experience with Python will be able to
explain better.

I'm not sure if this has cleared things up or just made them more confusing.

--
Mark Sparshatt




Reply via email to