PDD 15: Objects and Classes

Abstract

This PDD describes the semantics of Parrot's object and class systems.

Version

$Revision$

Definitions

Object

An object is a value that incorporates both data and behavior related to that data.

Class

A class defines a pattern of characteristics and behaviors from which objects are constructed.

Attribute

An attribute is a slot in an object that contains a value, generally a PMC. Attributes are referenced by class name/attribute name pairs.

Attributes are set on a class-wide basis, and all the objects of a class will have the same set of attributes. Most OO languages don't allow attribute changes to existing classes, but Parrot's base attribute system does allow it (only before the first object is instantiated from the class).

Method

A method is a piece of code that you invoke by name through an object. Methods implement the behaviour of an object.

Parent class

Also called the super-class. The parent class is, in an inheritance situation, the class being derived from. If A derives from B, B is the parent class of A.

Child class

Also called the sub-class. The child class is, in an inheritance situation, the class doing the deriving. If A derives from B, A is the child class.

Role

A role adds attributes and methods into a class without inheritance. The composed class retains a list of roles applied to it (so they can be checked with does), but otherwise maintains no distinction between composed attributes and methods and those defined in the class.

Delegate

An object that is transparently (to the user) embedded in another object. Delegate objects are used in those cases where we can't inherit from a class because the class is from a different object universe.

Property

A property is a role that only adds attributes and accessors.

Properties are generally assigned at runtime, and a particular property may or may not exist on a PMC at any particular time. Properties are not restricted to objects as such--any PMC may have a property attached to it.

Interface

An interface is a role that only adds methods.

Description

- The object and class system provides the flexibility to implement a core set of dynamic languages (Perl 6, Ruby, Python, etc). Other class systems may be implemented later to support other languages.
- Classes may have an associated namespace. (Anonymous classes are not associated with a namespace.)
- Classes may have one or more immediate parent classes
- Classes may have a catalog of attribute names.
- Classes may have a list of roles they implement
- Classes can instantiate an object of their class
- Classes can add and remove parent classes
- Classes can add and remove attributes
- Classes can add (but not remove) roles
- Classes are instances of a meta-class and have their own sets of class methods and class attributes
- Classes can be subclassed
- High-level classes can subclass low-level PMCs
- Objects have a collection (0 or more) of attributes. Attribute values may be PMCs or a low-level type.
- Objects have an associated class.
- Objects may have a custom vtable or use a class-wide vtable.
- Objects can call a method
- Objects can retrieve a method PMC for a method (for deferred method calls)
- Objects can fetch their class
- Objects can get an attribute by name
- Objects can set an attribute by name

Implementation

There are four pieces to the object implementation. There are the PMCs for the classes, roles, and objects, the opcodes the engine uses to do objecty things, specific vtable functions used to perform some of those objecty things, and the supporting code provided by the interpreter engine to do the heavy lifting.

Parrot, in general, doesn't restrict operations on objects and classes. If a language has restrictions on what can be done with them, the language is responsible for making sure that disallowed things do not happen. For example, Parrot permits multiple inheritance, and will not stop code that adds a new parent to an existing class. If a language doesn't allow for multiple inheritance it must not emit code which would add multiple parents to a class. (Parrot may, at some point, allow imposition of runtime restrictions on a class, but currently it doesn't.)

Class PMC API

There are two PMC classes, Class and Object. Class PMCs hold all the class-specific information. Instantiating a new high-level class creates a new Class PMC, and enters the new high-level class into Parrot's PMC class table, at which point it is indistinguishable from any other PMC class.

It's important to note that 'standard' classes are Class PMC instances, or instances of a subclass of the Class PMC, and 'standard' objects are Object PMCs. It isn't necessary to create a brand new low-level PMC class for each high-level class, and they all share the Class PMC vtable functions.

An instance of the Class PMC has eleven core internal attributes, which are:

  1. The class name
  2. A link to the class's associated namespace
  3. A "have I been instantiated since I was last modified" flag
  4. An array PMC of the immediate parent classes
  5. A cached array of all parent PMCs, in search order (this is an optional optimization, and can be calculated from the class's rules of inheritance, the list of immediate parent classes, and the parent classes' rules of inheritance)
  6. An array PMC of the composed roles (these are Role PMCs, not string names of roles).
  7. A hash PMC of the methods defined in the class or composed into the class
  8. A hash PMC of the overloaded PMC vtable entries for the class.
  9. The class attribute metadata hash. Keys are the attribute names and the values are a hash of attribute characteristics, including name, type, the class they're associated with and any flags (for example, private). Note that this only stores metadata for the attributes defined in this class, and not for attributes inherited from its parents.
  10. The full attribute lookup table. This associates attribute names with an index into the object's attribute storage (an array). It includes all attributes defined in the current class and every other class that it inherits from either directly or indirectly. The table is keyed on the name of the class where the attribute is defined, along with the attribute name. The value is an index into the per-object attribute store.
  11. The attribute cache. While the attribute lookup table defines every attribute, whether it is visible or not with the current method resolution order (MRO), the cache maps the names of the visible attributes directly to an index in the per- object attribute store. That saves a more costly lookup in the full attribute lookup table.

The attribute catalog holds only the attributes defined in a particular class. When instantiating an object, the object data store is created as a ResizablePMCArray, so doesn't need any specific details of the class's attribute structure. As attributes are set in the object (based on the index in the lookup table), the Array expands to accommodate the attribute indexes that are actually used. In the common case, a relatively small set near the lower index range is all that will be used.

When setting the attribute cache it is necessary to scan all parent classes as well as the instantiated class for attributes defined there. The inheritance rules (MRO) for a particular HLL will determine which child class attributes override which parent class attributes. The cache is only set on individual accesses to a particular attribute.

Class PMCs also have the "I am a class" flag set on them.

Classes, Namespaces, and the Class Registry

If a class hasn't been instantiated, adding a method or attribute modifies the existing class object. Extending a class that has been instantiated is not allowed.

The class registry has a much diminished role in this implementation. Its only responsibility is maintaining a mapping of unique IDs to class objects throughout the system. It should not be used for looking up classes by name.

The class registry may need to have names removed (since it doesn't care about names anymore). We plan to eventually eliminate the registry of class IDs altogether. Low-level PMC types also have entries in the namespace hierarchy via PMCProxy objects.

A class can be garbage collected when it has no instantiated objects and no Namespace object referencing it (to mark it as live). When a class is garbage collected, it should remove itself from the registry.

Class Vtable Entries

To make this work all Classes need the following vtable entries.

instantiate()
Instantiate a new object from the class. Set the instantiated flag on the class.
clone()
Create an (anonymous) clone of the class. Unset the instantiated flag on the new class.
name(string *)
Returns a simple string name for the class.
add_method(string *, method *)
Add a method to the class.
add_vtable_override(string *, vtable_sub *)
Add a vtable override to the class.
add_attribute(string *, key *)
Add an attribute to the class.
add_parent(class *)
Add a parent to the class.
add_role(role *)
Add a role to the class.
find_method(string *)
Returns the PMC for the named method. If no method of this name exists, nor can be constructed, returns a Null PMC.A class object reports on class methods, not on instance methods.
isa(pmc *)
Returns true or false if the class object, namespace object, key, or string name PMC passed in as a parameter is a class in the inheritance hierarchy of the object.
isa_str(string *)
Returns true or false if the string passed in as a parameter is a class in the inheritance hierarchy of the object.
can(string *)
Returns true or false if the class can perform the requested method. (Class systems that implement default fallback methods should report that they 'can' perform any method.)A class object reports on class methods, not on instance methods.
does(class *)
Returns true or false to note whether the class in question implements the interface passed in.A class object only reports on interfaces of the class (i.e. roles composed into the metaclass), it doesn't report on which interfaces will be added to an instance of that class.
inspect()
Return a data structure of all information relevant to introspection on the class.
inspect_str(string *)
Return a PMC Hash, Array, String, Integer, or Number value with introspection information corresponding to the requested string name. This may be overridden to report information about the internals of a class that aren't actually true (useful for mocking). It can also be used for straight introspection capabilities even when a particular class is using keyed access to act like a hash or array or attribute access to act as an object.
remove_attribute(string *)
Remove an attribute from the class.
remove_method(string *)
Remove a method from the class.
remove_parent(string *)
Remove a parent from the class.

Parrot only supports mutating class metainformation for Class classes. This restriction may be lifted at some point.

Class Methods

These methods are just syntactic sugar for the vtable functions.

name
    $P1 = $P2.'name'( $S3 )
The accessor for the name attribute. With no argument, it simply returns the current value for name. When passed an argument, it sets the name of the class, and also sets the association with a namespace. With no argument it only returns the current value of the name attribute.
get_namespace
    $P1 = $P2.'get_namespace'()
Retrieve the namespace object associated with the class.
new
    $P1 = $P2.'new'( 'myattrib' => "Foo" )
Create a new instance object from the class object. It takes an optional, slurpy, named list of attributes and values to initialize the object. Passing attribute names that weren't declared in the class is an error.
add_attribute
  $P1.'add_attribute'($S2)
  $P1.'add_attribute'($S2, $S3)
  $P1.'add_attribute'($S2, $P3)
Adds a single attribute to the class. It takes a simple string name and, optionally, a simple string value or key specifying a type name. (Types are not currently checked by Parrot, and only provided for introspection.)If the class has already been instantiated, adding a new attribute throws an exception.
attributes
  $P1 = $P2.'attributes'()
An accessor for the attributes of the class. It returns the a Hash of all attributes, with a key of the attribute name and a value of a Hash containing the attribute's metadata. The accessor is read-only.
add_method
  $P1.'add_method'($S2, $P3)
Adds a method to the class. It takes a simple string name and a method PMC. If the method already exists it will throw an exception.
methods
  $P1 = $P2.'methods'()
An accessor for the methods of the class. It returns a Hash of all methods, with a key of the method name and a value of an invokable PMC. Note that the methods list includes any methods that were composed into the class from roles.
add_vtable_override
  $P1.'add_vtable_override'($S2, $P3)
Adds a vtable override to the class. It takes a simple string name and a sub/method PMC. If the vtable override already exists it will throw an exception (attempting to add the same sub/method object a second time will be silently ignored).
add_parent
  $P1.'add_parent'($P3)
Adds a single parent to the class. It takes a Class PMC argument (the parent to add).
parents
  $P1 = $P2.'parents'()
An accessor for the parents of the class. It returns an array of all parents. The accessor is read-only.
roles
  $P1 = $P2.'roles'()
An accessor for the roles of the class. It returns an array of all roles. The accessor is read-only.
add_role
  $P1.'add_role'($P2, [named])
Adds a single role to the class. It takes an instance of the Role PMC as a required positional parameter, and the optional named parameters exclude and alias; see "Role Conflict Resolution" for more details.
subclass
  $P1 = $P2.'subclass'($S3)
Create a subclass of $P2 with name $S3 and return it in $P1.
isa
  $I1 = $P2.'isa'($S3)
Returns true if the class name passed in as a parameter is in the inheritance hierarchy of the class, false otherwise.
can
  $I1 = $P2.'can'($S3)
Returns true if the class object can perform the requested method, false otherwise.
does
  $I1 = $P2.'does'($S3)
Returns true if the object in question implements the role, class, type, or behavior passed in, false otherwise.A class object only reports on interfaces of the class (i.e. roles composed into the metaclass), while an instance object only reports on interfaces of the instance (i.e. roles composed into the class).
inspect
  $P1 = $P2.'inspect'()
  $P1 = $P2.'inspect'($S3)
Return introspection information for the class.

Object PMC API

Object PMCs are the actual objects, and hold all the per-object instance data.

An instance of the Object PMC has two core internal attributes, which are:

  1. The class PMC
  2. The object attribute store. This is simply an array of PMCs that provide the values for the attributes. It may be a resizable PMC array to provide lazy growth rather than allocating all needed memory for all attributes. The attribute cache and lookup table in the class store the indexes into this array, linking the attribute name and meta-information with the storage position.

A list of the object's attributes is accessible from the class. The attribute cache is the most straightforward way to retrieve a complete list of attributes visible to the object, but the first time you introspect for a complete list the class may have to calculate the list by traversing the inheritance hierarchy.

Object PMCs have the "I am an object" flag set on them.

Object PMCs have no methods aside from those defined in their associated class. They do have vtable functions providing access to certain low-level information about the object, method call functionality, etc. See the sections below on Objects and Vtables.

Object Vtable Entries

All Objects need the following vtable entries.

find_method(string *)
Returns the PMC for the named method. If no method of this name exists, nor can be constructed, returns a Null PMC.Note that for languages which support default fallback methods (such as Perl 5's AUTOLOAD) this would be the place to return it if a normal lookup fails.Since the method list and vtable override list are stored in the class PMC, method finding is a lookup on the class object and not a lookup in the namespace. Just adding a sub to a namespace will not automatically make it a method of the class, you have to call add_method too.An instance object reports on instance methods, not on class methods.
isa(class *)
Returns true or false if the class passed in as a parameter is in the inheritance hierarchy of the object.
can(string *)
Returns true or false if the object can perform the requested method. (Class systems that implement default fallback methods should report that they 'can' perform any method.)An instance object only reports on instance methods, not on class methods.
does(class *)
Returns true or false to note whether the object in question implements the interface passed in.An instance object only reports on interfaces of the instance (i.e. roles composed into the class).
get_attr(STRING*)
Returns the attribute with the string name for the object.
get_attr_keyed(PMC*, STRING*)
Returns the attribute with the string name for the object. Lookup is on the attribute store of a particular parent of the object's class, identified by a classname, namespace, or key PMC.
set_attr(STRING*, PMC*)
Set the attribute with the string name for the object.
set_attr_keyed(PMC*, STRING*, PMC*)
Set the attribute with the string name for the object. The value is set in the attribute store of a particular parent of the object's class, identified by a classname, namespace, or key PMC.
get_class
Returns the class PMC for the object.
clone
Create a clone of the object.
inspect()
Return a data structure of all information relevant to introspection on the object.
inspect_str(string *)
Return a PMC Hash, Array, String, Integer, or Number value of introspection information corresponding to the requested string name (such as 'parents'). This may be overridden to report information about the internals of an object that aren't actually true (useful for mocking). It can also be used for straight introspection capabilities even when a particular object is using keyed access (to act like a hash or array) or attribute access.

Role PMC API

An instance of the Role PMC has five core attributes, which are:

  1. The role name
  2. A link to the role's associated namespace
  3. An array PMC of composed roles
  4. An array PMC of the methods defined in the role or composed into the role
  5. The role attribute hash, where each key is an attribute name and the corresponding value is a hash of attribute characteristics, including name, type, and the role they're associated with.

Role Vtable Entries

All Roles need the following vtable entries.

add_method(string *, method *)
Add a method to the role.
add_vtable_override(string *, vtable_sub *)
Add a vtable override to the role.
add_attribute(string *, key *)
Add an attribute to the role.
add_role(role *)
Add a role to the role.
find_method(string *)
Returns the PMC for the named method. If no method of this name exists, nor can be constructed, returns a Null PMC.A role object reports on class methods (methods of the metarole), not on instance methods.
can(string *)
Returns true or false if the role can perform the requested method. (Class systems that implement default fallback methods should report that they 'can' perform any method.)A role object reports on class methods (methods of the metarole), not on instance methods.
does(class *)
Returns true or false to note whether the role in question implements the interface passed in.A role object only reports on interfaces of the role (i.e. roles composed into the metarole), it doesn't report on which interfaces will be added to an object instantiated from a class that composes the role.
clone
Create an (anonymous) clone of the role.
inspect()
Return a data structure of all information relevant to introspection on the role.
inspect_str(string *)
Return a PMC Hash, Array, String, Integer, or Number value of introspection information corresponding to the requested string name (such as 'parents').
remove_attribute(string *)
Remove an attribute from the role.
remove_method(string *)
Remove a method from the role.

Role Methods

These methods are just syntactic sugar for the vtable functions.

name
    $P1 = $P2.'name'( $S3 )
The accessor for the name attribute. With no argument, it simply returns the current value for name. When passed an argument, it sets the name of the role, and also sets the association with a namespace.
get_namespace
    $P1 = $P2.'get_namespace'()
Retrieve the namespace object associated with the role.
attributes
  $P1 = $P2.'attributes'()
An accessor for the attributes of the role. It returns the Hash of all attributes, with a key of the attribute name, and a value of the attribute's metadata (a Hash). The accessor is read-only.
add_attribute
  $P1.'add_attribute'($S2)
  $P1.'add_attribute'($S2, $S3)
  $P1.'add_attribute'($S2, $P3)
Adds a single attribute to the role. It takes a simple string name, and optionally, a simple string value or key specifying a type name. (A type name just checks does, and doesn't necessarily correspond to a class or role namespace.)
add_role
  $P1.'add_role'($P2, [named])
Adds a single role to the role. It takes an instance of the Role PMC as a required positional parameter, and the optional named parameters exclude and alias; see "Role Conflict Resolution" for more details.
roles
  $P1 = $P2.'roles'()
An accessor for the roles composed into the role. It returns an Array of all roles as PMC objects. If any roles that were composed into this one were themselves made up of a composition of other roles, the roles they were made up of will also be included in the value returned by this accessor. However, no role will be mentioned more than once. The accessor is read-only.
add_method
  $P1.'add_method'($S2, $P3)
Adds a method to the role. It takes a simple string name and a method PMC. If the method already exists it will throw an exception.
add_vtable_override
  $P1.'add_vtable_override'($S2, $P3)
Adds a vtable override to the role. It takes a simple string name and a sub/method PMC. If the vtable override already exists it will throw an exception.
methods
  $P1 = $P2.'methods'()
An accessor for the methods of the role. It returns a Hash of all methods, with a key of the method name and a value of an invokable PMC. The list will include methods added through composing other roles into this role. The accessor is read-only.
inspect
  $P1 = $P2.'inspect'()
  $P1 = $P2.'inspect'($S3)
Return introspection information for the role.

Role Conflict Resolution

When a role is added to a class, we try to compose it right away, and throw an exception on any conflicts that are detected. A conflict occurs if two roles try to supply a method of the same name (but see the note on multi-methods below). High level languages will provide varying facilities to deal with this, and Parrot provides the primitives to implement them.

When declaring a composed class, you can optionally supply an array of method names that will be defined by the class to resolve a conflict in its roles. This is done using the named parameter resolve. This feature supports composition conflict resolution.

When adding a role to a class, you can optionally supply an array of method names from the role to exclude from the composition process. This is done using the named parameter exclude. It is not an error to list a method name in this array that the role does not have. This makes it possible to implement languages that provide for explicit exclusions on a role-by-role basis.

When adding a role to a class, you can optionally specify that specific methods are to be aliased to different names within the class. This is done with the optional alias named parameter. The parameter takes hash of strings, where the key is a method name in the role, and the value is the name it will have in to the class. (This is also sometimes used for conflict resolution.)

If you alias a method, it won't automatically exclude the original name from the role. You can also explicitly exclude the method name, if you want a proper renaming of the method. A resolve at the class level will automatically exclude all methods of that name from any role composed into the class. You can alias the method if you want to call it from the composed class. (You might use this if you want the resolving method to be able to call either of the conflicting methods from two composed roles.)

Opcodes

The following ops are provided to deal with objects. Please note that method calls are governed by Parrot's calling conventions, and as such objects, method PMCs, return continuations, and parameters must be in the right places, though some ops will put parameters where they need to go.

getattribute
  $P1 = getattribute $P2, $S3
  $P1 = getattribute $P2, $P3, $S4
Get the attribute with the fully qualified name $S3 from object $P2 and put it in $P1. To get an attribute for a parent class that has the same name as an attribute in the child class, pass an optional class object or namespace key $P3 for the parent class.If the attribute doesn't exist, it will throw an exception. If the attribute exists, but the value hasn't been set, it will return a null PMC.
setattribute
  setattribute $P1, $S2, $P3
  setattribute $P1, $P2, $S3, $P4
Set the attribute of object $P1 with the attribute name $S2 to $P3. To set an attribute for a parent class that has the same name as an attribute in the child class, pass an optional class object or namespace key $P2 for the parent class.If the attribute doesn't exist, it will throw an exception.
callmethod
  callmethod $P1, $S1, $P2
Call the method specified in the string name $S1 using $P1 as the invocant and using the continuation passed in $P2. If you need to create a new continuation use callmethodcc.
  callmethod $P1, $P2, $P3
Call the method specified in the Sub object $P2 using $P1 as the invocant and using the continuation passed in $P3. If you need to create a new continuation use callmethodcc.
callmethodcc
  callmethodcc $P1, $S1
  callmethodcc $P1, $P2
Call the method specified in the string name $S1, or in the Sub object $P2, using $P1 as the invocant for method lookup and generate a new return continuation.Throws an exception for a non-existent method.
callmethodsupercc [hypothetical, 2.0 or later]
Call the method specified in the string name $S1, or in the Sub object $P2, using $P1 as the invocant for method lookup and generate a new return continuation. This is a variant of callmethodcc that skips over the current class when searching for the method, and only looks in the parent classes. PIR may provide some syntactic sugar for this.
callmethodnextcc [hypothetical, 2.0 or later]
Call the method specified in the string name $S1, or in the Sub object $P2, using $P1 as the invocant for method lookup and generate a new return continuation. A variant of callmethodcc that picks up an existing find_method search where it left off for the current call. {{ Note: this depends on find_method being resumable, and on the context of a particular method including a pointer to the find_method call that found it. Neither may be feasible. }} PIR may provide some syntactic sugar for this.
newclass
  $P1 = newclass $S2
  $P1 = newclass $S2, $P3
Create a new base class named $S2, and put the PMC for it in $P1. You may optionally pass a hash of initialization parameters for the class in $P3.
subclass
  $P1 = subclass $S2
  $P1 = subclass $P2
  $P1 = subclass $S2, $S3
  $P1 = subclass $P2, $S3
  $P1 = subclass $S2, $P3
  $P1 = subclass $P2, $P3
Create a new class, named $S3, which has $P2 as its immediate parent. $P2 may be either another high-level class based on the Class PMC, or it may be a low-level PMC such as Integer or ResizablePMCArray.
get_class
  $P1 = get_class $S2
  $P1 = get_class $P2
Retrieve a class object for the class identified by the string name in $S2, or by the PMC key or namespace object in $P2.A string name looks for the class in a namespace with that name nested in the currently selected namespace. Passing in a namespace object looks for the class in that namespace object. A key looks for the class in the namespace identified by the multilevel key relative to the currently selected HLL.If the class doesn't exist, it returns a null PMC.
typeof
  $S1 = typeof $P2
  $P1 = typeof $P2
Lookup the type of the instance object in $P2. Return the string name if the destination is a string register or variable. If the destination is a PMC register or variable, return the class object for an instance of a high-level class, or the class proxy object for an instance of a low-level PMC.
new
  $P1 = new $S2
  $P1 = new $S2, $P3
  $P1 = new $P2
  $P1 = new $P2, $P3
Create a new object from the class named by $S2 or $P2 (a string PMC, namespace key, or class object), and put the PMC for it in $P1. You may optionally pass a hash of initialization parameters for the class in $P3.
addparent
  addparent $P1, $P2
Add class $P2 to the end of the list of immediate parents of class $P1. Adds any attributes of $P2 (and its parent classes) that aren't already in $P1.
removeparent
  removeparent $P1, $P2
Remove class $P2 from the parent list of $P1. All parent classes of $P2 which aren't parent classes of what remains of $P1's parent list are removed, as are their attributes.
addattribute
  addattribute $P1, $S2
  addattribute $P1, $S2, $S3
  addattribute $P1, $S2, $P3
Add attribute $S2 to class or role $P1. This will add the attribute slot to all objects of class $P1, classes that inherit from class $P1, or classes that compose the role $P1, with a default value of Null. It optionally takes a simple string value or key specifying a type of the attribute.
removeattribute
  removeattribute $P1, $S2
Remove the attribute $S2 from class or role $P1. This will not remove the attribute from any objects that have already been instantiated, but it will be absent from all future objects of class $P1, of classes that inherit from class $P1, or of classes that compose the role $P1.
addrole
  addrole $P1, $P2
Add role $P2 to the end of the list of roles of class or role $P1. Adds any methods and attributes of $P2 that aren't already in $P1.
inspect
  $P1 = inspect $P2
  $P1 = inspect $P2, $S3
Return introspection information for the PMC. Without the optional string argument, return a data structure of all information relevant to introspection. With the optional string argument, return a PMC Hash, Array, String, Integer, or Number value with introspection information corresponding to the requested string name.

PIR Class Definitions

PIR provides some syntactic sugar for declaring classes.

:method
  .sub custom_method :method
    # ...
  .end
Flags the code entity as a method.
:vtable
  .sub get_integer :vtable
    # ...
  .end
Flags the code entity as a vtable override.

:method and :vtable can be combined to indicate that a particular code entity is callable both as a method and as a vtable override.

If the class object has not yet been created at the point when the PIR subs are compiled, the methods and vtable overrides are temporarily stored in the associated namespace.

Vtable Overriding

Classes can override vtable functions from PIR, allowing control over the low-level behavior of objects similar to PMCs defined in C. The vtable functions all take a single fixed argument list, and return at most a single value. Calling the overrides directly requires some knowledge of the way Parrot works, so higher-level languages will generally provide easier-to-use wrappers.

To override a vtable function, either add the :vtable pragma to the declaration of the method, or pass a named parameter "vtable" into the add_method method on a class or role.

Examples

The following examples all assume we're working with basic Object objects and Class classes.

Creating a new class

To create a new class Foo which has no parent classes:

   $P0 = newclass "Foo"

Creating a new class with multiple parents

To create a class Foo with the parents A and B, the code would be:

   $P0 = get_class "A"
   $P1 = get_class "B"
   $P2 = subclass $P0, "Foo"
   addparent $P2, $P1

Creating a new class with attributes

Adding the attributes a and b to the new class Foo:

  $P0 = newclass "Foo"
  addattribute $P0, "a"
  addattribute $P0, "b"

Instantiating an object

Assuming we want an object of class Foo:

  .local pmc FooClass
  .local pmc MyObject
  FooClass = get_class "Foo"
  MyObject = FooClass.'new'()

Calling a method on an object

Calling the method Xyzzy on an object, assuming the PDD03 calling conventions are respected:

Or, if a return continuation needs constructing:

Or, calling a method in PIR, where the calling conventions are handled automatically.

  $P0.'Xyzzy'($P1)

Accessing attributes from within a class

With named access:

  $P1 = getattribute $P0, "b"

Explanations

To get a new class, you can do a newclass, which creates a new class with no parents besides Parrot's default super-ish parent class.

To get a new child class, you have two potential options:

Subclass the parent
Create a new standalone class and add a parent

Both ways work. It is, however, more efficient to use the first method, and just subclass the immediate parent class of your new class.

When adding in extra parents in a multiple-inheritance scenario, subclass the first class in the immediate parent list then use the addparent op to add in the rest of the immediate parents.

Language Notes

Notes on some of the OO-related needs of various languages.

PMCs

Ruby: Just like Smalltalk, everything is an object. Core Ruby classes (String, Array, Hash, Module, etc) might be implemented something like this:

 ParrotClass
    |
 RubyClass   String
    |         |
     \      /
    RubyString

Objectspace

Ruby: Objectspace in Ruby allows the programmer to iterate through every live object in the system. There is some debate about how to make this play nice with different garbage collection schemes.

Classes

A class is a collection of methods and attributes. It would be desirable, for those classes whose definition is fully known at compile time, to have a convenient way to have the class along with its attributes and methods stored into a PBC file rather than created at runtime. However, creation of new classes at runtime will be needed too.

Meta-classes

Ruby: Ruby has meta-classes. It would be nice if classes were objects in Parrot's OO model.

Attributes

Attributes are instance data associated with a class (or role, however those are supported). They may not always be of a type specified by a PMC, though boxing/unboxing is of course an option.

Perl 6: All attributes are opaque (not externally visible, even to any subclasses).

.Net: Attributes may be private (not externally visible), public (always externally visible), protected (only visible to subclasses) and internal (only visible inside the current assembly - the closest correspondence in Parrot is perhaps only visible inside the same PBC file). Additionally, it is allowable for a subclass to introduce an attribute of the same name as the a parent class has, and they both exist depending on what type an instance of the class is currently viewed as being (read: there is a difference between the type of the reference and the type of the value).

Ruby: Attributes can be dynamically added and removed at runtime.

Methods

Perl 6: Methods may be public (anyone can invoke them) or private (only invokable by the class they are defined in). Additionally, submethods are methods that do not get inherited.

.Net: Like attributes, methods may be public, private, protected or internal.

Ruby: has a method_missing that gets called when method resolution fails to find a method. Methods can be dynamically added and removed at runtime.

Constructors

A constructor is run when an object is instantiated.

.Net: There may be many constructors for an object (provided they all have different signatures), and the correct one is called based upon the passed parameters.

Inheritance

Perl 6: Multiple inheritance.

.Net: Single inheritance.

Ruby: Single inheritance but support for mixins of Ruby modules.

Interfaces

An interface specifies a set of methods that must be implemented by a class that inherits (or implements) the interface, but does not provide any form of implementation for them.

.Net: Interfaces are pretty much what was just describe above. XXX Need to check behavior of you implement two interfaces with methods of the same name.

Roles

A role consists of a set of methods and attributes. It cannot be instantiated on its own, but must be composed into a class. When this happens its methods and attributes become of that classes methods and attributes. This may happen at compile time or runtime, however when a role is composed into a class at runtime then what really happens is that a new anonymous class is created with the role composed into it and then the namespace entry for the existing class is updated to refer to the new one. Note that this means classes must be garbage collectable, with all those referred to by a namespace or with objects of that class existing being marked live.

Perl 6: Roles pretty much are a Perl 6 thing, so the definition above contains all that is needed. An open question is whether Parrot worry about collision detection? For compile time composition that's easy to punt to the compiler; for runtime composition, that's not so easy though.

Introspection (aka Reflection)

Perl 6: Reflection provides access to a list of methods that a class has, its parent classes and the roles it does, as well as the name of the class and its memory address. For methods, their name, signature, return type and whether the method is declared multi are available.

.Net: Reflection provides access to a list of attributes and methods as well as the name of the class and its parent. The types of attributes and signatures of methods are also available.

Inner Classes

An inner class is essentially a class defined within a class. Therefore it has access to things private to its outer class.

Perl 6: Inner classes are allowed, and may also be private.

.Net: Inner classes are allowed and may be private, public, protected or internal.

Delegation

Delegation is where a method call is "forwarded" to another class. Parrot may provide support for simple cases of it directly, or could just provide a "no method matched" fallback method that the compiler fills out to implement the delegation.

Perl 6: Delegation support is highly flexible, even allowing a regex to match method names that should be delegated to a particular object.

Prototype-based OO

Prototype-based OO has no classes. All objects are cloned from existing objects and modified. Requires lightweight singleton creation, without needing a separate class for every instance object. (Self, JavaScript, and Io are examples of prototype-based 00.) An example from Io:

  Dog := Object clone # The Dog object is a clone of Object
  Dog tail := "long"  # it has an attribute 'tail' with the value 'long'
  Dog bark := method("yap" print) # It has a method 'bark'
  Dog bark  # call the method 'bark', printing 'yap'

Translation

The following list a set of languages, then within each language what the Parrot term translates to.

Python
Attribute
A Python attribute maps to a Parrot property
.NET
Attribute
What .NET calls an attribute Parrot calls a property
Property
What .NET calls a property we call an attribute
Generic Terminology
Instance Variable
Instance Variables map to what we call attributes

Attachments

  docs/pdds/pdd15_object_metamodel.png

Footnotes

None.

References

None.