P6object - Perl 6-like methods and metaclasses for Parrot
.sub 'main' load_bytecode "dumper.pbc" # load this library load_bytecode 'P6object.pbc' ## grab the P6metaclass protoobject .local pmc p6meta p6meta = get_hll_global 'P6metaclass' ## create a new class ABC::Def with three attributes p6meta.'new_class'('ABC::Def', 'attr'=>'$a @b %c') ## get the protoobject for ABC::Def .local pmc defproto defproto = get_hll_global ['ABC'], 'Def' ## use the protoobject to create a new ABC::Def object .local pmc obj obj = defproto.'new'() ## get the class protoobject from any object $P0 = obj.'WHAT'() ## get the metaclass for any object $P0 = obj.'HOW'() ## create a new class MyHash as a subclass of Parrot's 'Hash' p6meta.'new_class'('MyHash', 'parent'=>'Hash') ## tell Parrot classes to use a specific protoobject $P0 = get_hll_global 'MyHash' p6meta.'register'('Hash', 'protoobject'=>$P0) $P1 = new 'Hash' # create a Hash $P2 = $P1.'WHAT'() # get its protoobject $S3 = $P2 # stringify say $S3 # "MyHash\n"
P6object is intended to add Perl 6-like behaviors to objects in Parrot. It creates and maintains protoobjects, and supplies
.HOW methods to objects and protoobjects in Parrot. Protoobjects also have a default
.new method for creating new instances of a class (classes are able to override this, however).
P6object is the base class for objects that make use of the P6metamodel. It supplies the
Initializes the P6object system. Builds protoobjects for
P6metaclass of the invocant.
P6protoobject for the invocant.
Return the memory address for the invocant.
Return the package for the object.
Return a list of methods to be overridden in protoobjects for the class. Defaults to 'new' (i.e., any '.new' method in a class will override the one given for P6protoobject below).
Return a true value if the invocant 'isa'
Return a true value if the invocant 'can'
Deprecated; use add_parent(class, parentclass)
DEPRECATED. Use add_method(class, name, method)
DEPRECATED. Use compose_role(class, role)
Sets objects of type
parrotclass to use
protoobject, and verifies that
parrotclass has P6object methods defined on it. This happens either by setting
P6object as a parent of
parrotclass, or by individually composing
P6object's methods into
name parameter causes objects to be registered using a name that differs from the parrotclass name. This is useful when needing to map to a class name that already exists in Parrot (e.g., 'Hash' or 'Object').
how parameter allows you to specify an already-existing metaclass instance to be used for this class rather than creating a new one.
Create a new class called
name as a subclass of
name is a string, then double-colons will be treated as separators. If
parentclass isn't supplied, defaults to using
P6object as the parent. The
attr parameter is a list of attribute names to be added to the class, specified as either an array or a string of names separated by spaces.
Retrieve the protoobject for
name. Return null if no protoobject exists, or whatever is present isn't a protoobject.
Multimethod helper to return the parrotclass for
Returns the "shortname" of the protoobject's class and parens.
Protoobjects are always treated as being undefined.
Have protoobjects return their longname in response to a
Provides a default constructor for creating objects in the class.
Note that unlike Perl 6, the
new method here exists only in the protoobject and not in the individual instances of the class. (If you want all objects in a class to have a
new method, then define one in the class and it will be used in lieu of this one.)
Written and maintained by Patrick R. Michaud,
pmichaud at pobox.com. Please send patches, feedback, and suggestions to the parrot-dev mailing list or to
Copyright (C) 2008, Parrot Foundation.