NAME
P6object - Perl 6-like methods and metaclasses for Parrot
SYNOPSIS
.sub 'main'
# 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"
DESCRIPTION
P6object is intended to add Perl 6-like behaviors to objects in Parrot. It creates and maintains protoobjects, and supplies .WHAT and .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).
CLASSES
P6object
P6object is the base class for objects that make use of the P6metamodel. It supplies the .WHAT and .HOW methods.
- onload() :anon :init :load Initializes the P6object system. Builds protoobjects for
- HOW() Return the
- WHAT() Return the
- PROTOOVERRIDES() 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).
P6object and P6metaclass.
P6metaclass of the invocant.
P6protoobject for the invocant.
P6metaclass
- WHAT() Return the protoobject for this metaclass.
- isa(x) Return a true value if the invocant 'isa'
- can(x) Return a true value if the invocant 'can'
- add_parent(parentclass [, 'to'=>parrotclass])
- add_method(name, method, [, 'to'=>parrotclass]) Add
- add_role(role, [, 'to'=>parrotclass]) Add
- register(parrotclass [, 'name'=>name] [, 'protoobject'=>proto] [, 'parent'=>parentclass] [, 'hll'=>hll]) Sets objects of type
- new_class(name [, 'parent'=>parentclass] [, 'attr'=>attr] [, 'hll'=>hll]) Create a new class called
- get_proto(name) Retrieve the protoobject for
- get_parrotclass(x) Multimethod helper to return the parrotclass for
x.
x.
method with name to parrotclass.
role to parrotclass.
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 parrotclass.The 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').
name as a subclass of parentclass. When 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.
name. Return null if no protoobject exists, or whatever is present isn't a protoobject.
x.P6protoobject
- get_string() (vtable method) Returns the "shortname" of the protoobject's class.
- defined() (vtable method) Protoobjects are always treated as being undefined.
- name() (vtable method) Have protoobjects return their longname in response to a
- new() Provides a default constructor for creating objects in the class.Note that unlike Perl 6, the
- ACCEPTS(topic)
typeof_s_p opcode.
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.)
AUTHOR
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 parrotbug@parrotcode.org .
COPYRIGHT
Copyright (C) 2008, Parrot Foundation.