160 likes | 328 Views
OO in Parrot. Dan Sugalski Dan@sidhe.org. April 14, 2003. Goals of Parrot OO. Support perl 5’s “anything goes” style of objects Support perl 6’s “atrributes and compile-time fixednes” style of objects Support Ruby and Python objects All interchangeably. OO in four parts.
E N D
OO in Parrot Dan Sugalski Dan@sidhe.org April 14, 2003
Goals of Parrot OO • Support perl 5’s “anything goes” style of objects • Support perl 6’s “atrributes and compile-time fixednes” style of objects • Support Ruby and Python objects • All interchangeably
OO in four parts • Outside code using objects • Code within parrot-style object classes • Class mutation • Inheritance
Objects from the outside • Objects are opaque • They have properties • You can call methods on them • Other than that, don’t touch
Calling a method Perl 6 $obj.foo();
Calling a method Parrot Assembly clearall find_global P2, “$obj” set S0, “foo” callmeth
Calling a method Parrot Assembly (Fast version) clearall find_global P2, “$obj” set S0, “foo” callmeth .foo_hashed
Parrot class objects • Objects are arrays of attributes • One slot per attribute of the class and all compatible parent classes • Most metadata about objects is stored in the class for the object. • This includes slot directories and such
Mutating classes • Adding attributes at runtime is possible • We’re building a full notification and event system into Parrot • Blows caches like mad, so try to avoid this
Inheritance • You can inherit from any class • Inheriting from ‘foreign’ classes does transparent delegation • Delegated objects have parent properties for proper redispatch • Opaque classes are a bit of a dead-end
New for Method Invocation • Prototypes enforced for methods • Method vs Sub invocation known • Multimethod dispatch • Guaranteed passed-in method names • Method tail calls
Introspection • No promises in general • In practice, lots of opportunities • Base object type is completely inspectable
Engine goodies • Full notification and event system • Method caching (lexically scoped!) • Core hash calculations • Multimethod dispatch handling • Everything can be overridden
Property fetching Property storing Returned property hashes Attribute get Attribute set can does isa AUTOLOAD Method lookup Method dispatch Pre and post method calling Really, everything
Both kinds of objects • Both reference and value types • Objects have full control over their vtables • Full control is yours if you want it • Wraps up tying, overloading, and magic • Oddly enough, all the object stuff is done as just a custom PMC class--no core changes were needed • We’re making some anyway, though
Our plans for world domination • Perl 6, Python, and Ruby objects all use the new scheme • Transparent inheritance/delegation for perl 5 support