1 # Copyright (C) 2001-2010, Parrot Foundation.
4 =head1 PDD 15: Objects and Classes
8 This PDD describes the semantics of Parrot's object and class systems.
18 An object is a value that incorporates both data and behavior
23 A class defines a pattern of characteristics and behaviors from which
24 objects are constructed.
28 An attribute is a slot in an object that contains a value, generally a
29 PMC. Attributes are referenced by class name/attribute name pairs.
31 Attributes are set on a class-wide basis, and all the objects of a class
32 will have the same set of attributes. Most OO languages don't allow
33 attribute changes to existing classes, but Parrot's base attribute
34 system does allow it (only before the first object is instantiated from
39 A method is a piece of code that you invoke by name through an object.
40 Methods implement the behaviour of an object.
44 Also called the super-class. The parent class is, in an inheritance situation,
45 the class being derived from. If A derives from B, B is the parent class of A.
49 Also called the sub-class. The child class is, in an inheritance situation,
50 the class doing the deriving. If A derives from B, A is the child class.
54 A role adds attributes and methods into a class without inheritance. The
55 composed class retains a list of roles applied to it (so they can be
56 checked with C<does>), but otherwise maintains no distinction between
57 composed attributes and methods and those defined in the class.
61 An object that is transparently (to the user) embedded in another object.
62 Delegate objects are used in those cases where we can't inherit from a class
63 because the class is from a different object universe.
67 A property is a role that only adds attributes and accessors.
69 Properties are generally assigned at runtime, and a particular property may or
70 may not exist on a PMC at any particular time. Properties are not restricted
71 to objects as such--any PMC may have a property attached to it.
75 An interface is a role that only adds methods.
81 =item - The object and class system provides the flexibility to
82 implement a core set of dynamic languages (Perl 6, Ruby, Python, etc).
83 Other class systems may be implemented later to support other languages.
85 =item - Classes may have an associated namespace. (Anonymous classes are
86 not associated with a namespace.)
88 =item - Classes may have one or more immediate parent classes
90 =item - Classes may have a catalog of attribute names.
92 =item - Classes may have a list of roles they implement
94 =item - Classes can instantiate an object of their class
96 =item - Classes can add and remove parent classes
98 =item - Classes can add and remove attributes
100 =item - Classes can add (but not remove) roles
102 =item - Classes are instances of a meta-class and have their own sets of
103 class methods and class attributes
105 =item - Classes can be subclassed
107 =item - High-level classes can subclass low-level PMCs
109 =item - Objects have a collection (0 or more) of attributes. Attribute
110 values may be PMCs or a low-level type.
112 =item - Objects have an associated class.
114 =item - Objects may have a custom vtable or use a class-wide vtable.
116 =item - Objects can call a method
118 =item - Objects can retrieve a method PMC for a method (for deferred
121 =item - Objects can fetch their class
123 =item - Objects can get an attribute by name
125 =item - Objects can set an attribute by name
129 =head2 Implementation
131 There are four pieces to the object implementation. There are the PMCs for the
132 classes, roles, and objects, the opcodes the engine uses to do objecty things,
133 specific vtable functions used to perform some of those objecty things, and
134 the supporting code provided by the interpreter engine to do the heavy
137 Parrot, in general, doesn't restrict operations on objects and classes. If a
138 language has restrictions on what can be done with them, the language is
139 responsible for making sure that disallowed things do not happen. For
140 example, Parrot permits multiple inheritance, and will not stop code that adds
141 a new parent to an existing class. If a language doesn't allow for multiple
142 inheritance it must not emit code which would add multiple parents to a class.
143 (Parrot may, at some point, allow imposition of runtime restrictions on a
144 class, but currently it doesn't.)
149 There are two PMC classes, C<Class> and C<Object>. Class PMCs hold all
150 the class-specific information. Instantiating a new high-level class
151 creates a new Class PMC, and enters the new high-level class into
152 Parrot's PMC class table, at which point it is indistinguishable from
155 It's important to note that 'standard' classes are Class PMC instances,
156 or instances of a subclass of the Class PMC, and 'standard' objects are
157 Object PMCs. It isn't necessary to create a brand new low-level PMC
158 class for each high-level class, and they all share the Class PMC
161 An instance of the Class PMC has eleven core internal attributes, which
172 A link to the class's associated namespace
176 A "have I been instantiated since I was last modified" flag
180 An array PMC of the immediate parent classes
184 A cached array of all parent PMCs, in search order (this is an optional
185 optimization, and can be calculated from the class's rules of inheritance,
186 the list of immediate parent classes, and the parent classes' rules of
191 An array PMC of the composed roles (these are Role PMCs, not string names of
196 A hash PMC of the methods defined in the class or composed into the
201 A hash PMC of the overloaded PMC vtable entries for the class.
205 The class attribute metadata hash. Keys are the attribute names and the
206 values are a hash of attribute characteristics, including name, type, the
207 class they're associated with and any flags (for example, private). Note
208 that this only stores metadata for the attributes defined in this class,
209 and not for attributes inherited from its parents.
213 The full attribute lookup table. This associates attribute names with an
214 index into the object's attribute storage (an array). It includes all
215 attributes defined in the current class and every other class that it
216 inherits from either directly or indirectly. The table is keyed on the
217 name of the class where the attribute is defined, along with the
218 attribute name. The value is an index into the per-object attribute
223 The attribute cache. While the attribute lookup table defines every
224 attribute, whether it is visible or not with the current method
225 resolution order (MRO), the cache maps the names of the visible
226 attributes directly to an index in the per- object attribute store. That
227 saves a more costly lookup in the full attribute lookup table.
231 The attribute catalog holds only the attributes defined in a particular
232 class. When instantiating an object, the object data store is created as
233 a ResizablePMCArray, so doesn't need any specific details of the class's
234 attribute structure. As attributes are set in the object (based on the
235 index in the lookup table), the Array expands to accommodate the
236 attribute indexes that are actually used. In the common case, a
237 relatively small set near the lower index range is all that will be
240 When setting the attribute cache it is necessary to scan all parent
241 classes as well as the instantiated class for attributes defined there.
242 The inheritance rules (MRO) for a particular HLL will determine which
243 child class attributes override which parent class attributes. The
244 cache is only set on individual accesses to a particular attribute.
246 Class PMCs also have the "I am a class" flag set on them.
248 =head4 Classes, Namespaces, and the Class Registry
250 If a class hasn't been instantiated, adding a method or attribute
251 modifies the existing class object. Extending a class that has been
252 instantiated is not allowed.
254 The class registry has a much diminished role in this implementation.
255 Its only responsibility is maintaining a mapping of unique IDs to class
256 objects throughout the system. It should not be used for looking up
259 The class registry may need to have names removed (since it doesn't care
260 about names anymore). We plan to eventually eliminate the registry of
261 class IDs altogether. Low-level PMC types also have entries in the
262 namespace hierarchy via PMCProxy objects.
264 A class can be garbage collected when it has no instantiated objects
265 and no Namespace object referencing it (to mark it as live). When a
266 class is garbage collected, it should remove itself from the registry.
269 =head4 Class Vtable Entries
271 To make this work all Classes need the following vtable entries.
277 Instantiate a new object from the class. Set the instantiated flag on the
282 Create an (anonymous) clone of the class. Unset the instantiated flag on the
287 Returns a simple string name for the class.
289 =item add_method(string *, method *)
291 Add a method to the class.
293 =item add_vtable_override(string *, vtable_sub *)
295 Add a vtable override to the class.
297 =item add_attribute(string *, key *)
299 Add an attribute to the class.
301 =item add_parent(class *)
303 Add a parent to the class.
305 =item add_role(role *)
307 Add a role to the class.
309 =item find_method(string *)
311 Returns the PMC for the named method. If no method of this name exists, nor
312 can be constructed, returns a Null PMC.
314 A class object reports on class methods, not on instance methods.
318 Returns true or false if the class object, namespace object, key, or
319 string name PMC passed in as a parameter is a class in the inheritance
320 hierarchy of the object.
322 =item isa_str(string *)
324 Returns true or false if the string passed in as a parameter is a
325 class in the inheritance hierarchy of the object.
329 Returns true or false if the class can perform the requested method.
330 (Class systems that implement default fallback methods should report
331 that they 'can' perform any method.)
333 A class object reports on class methods, not on instance methods.
337 Returns true or false to note whether the class in question implements the
340 A class object only reports on interfaces of the class (i.e. roles
341 composed into the metaclass), it doesn't report on which interfaces will
342 be added to an instance of that class.
346 Return a data structure of all information relevant to introspection on
349 =item inspect_str(string *)
351 Return a PMC Hash, Array, String, Integer, or Number value with
352 introspection information corresponding to the requested string name.
353 This may be overridden to report information about the internals of a
354 class that aren't actually true (useful for mocking). It can also be
355 used for straight introspection capabilities even when a particular
356 class is using keyed access to act like a hash or array or attribute
357 access to act as an object.
359 =item remove_attribute(string *)
361 Remove an attribute from the class.
363 =item remove_method(string *)
365 Remove a method from the class.
367 =item remove_parent(string *)
369 Remove a parent from the class.
373 Parrot only supports mutating class metainformation for Class classes.
374 This restriction may be lifted at some point.
378 These methods are just syntactic sugar for the vtable functions.
386 $P1 = $P2.'name'( $S3 )
390 The accessor for the name attribute. With no argument, it simply returns
391 the current value for name. When passed an argument, it sets the name of
392 the class, and also sets the association with a namespace. With no
393 argument it only returns the current value of the name attribute.
399 $P1 = $P2.'get_namespace'()
403 Retrieve the namespace object associated with the class.
409 $P1 = $P2.'new'( 'myattrib' => "Foo" )
413 Create a new instance object from the class object. It takes an optional,
414 slurpy, named list of attributes and values to initialize the object.
415 Passing attribute names that weren't declared in the class is an error.
421 $P1.'add_attribute'($S2)
422 $P1.'add_attribute'($S2, $S3)
423 $P1.'add_attribute'($S2, $P3)
427 Adds a single attribute to the class. It takes a simple string name and,
428 optionally, a simple string value or key specifying a type name. (Types
429 are not currently checked by Parrot, and only provided for
432 If the class has already been instantiated, adding a new attribute
439 $P1 = $P2.'attributes'()
443 An accessor for the attributes of the class. It returns the a Hash of
444 all attributes, with a key of the attribute name and a value of a Hash
445 containing the attribute's metadata. The accessor is read-only.
451 $P1.'add_method'($S2, $P3)
455 Adds a method to the class. It takes a simple string name and a method
456 PMC. If the method already exists it will throw an exception.
462 $P1 = $P2.'methods'()
466 An accessor for the methods of the class. It returns a Hash of all
467 methods, with a key of the method name and a value of an invokable PMC.
468 Note that the methods list includes any methods that were composed into
469 the class from roles.
471 =item add_vtable_override
475 $P1.'add_vtable_override'($S2, $P3)
479 Adds a vtable override to the class. It takes a simple string name and a
480 sub/method PMC. If the vtable override already exists it will throw an
481 exception (attempting to add the same sub/method object a second time
482 will be silently ignored).
488 $P1.'add_parent'($P3)
492 Adds a single parent to the class. It takes a Class PMC argument (the
499 $P1 = $P2.'parents'()
503 An accessor for the parents of the class. It returns an array of all
504 parents. The accessor is read-only.
514 An accessor for the roles of the class. It returns an array of all
515 roles. The accessor is read-only.
519 =begin PIR_FRAGMENT_INVALID
521 $P1.'add_role'($P2, [named])
523 =end PIR_FRAGMENT_INVALID
525 Adds a single role to the class. It takes an instance of the Role PMC as a
526 required positional parameter, and the optional named parameters C<exclude>
527 and C<alias>; see L<Role Conflict Resolution> for more details.
533 $P1 = $P2.'subclass'($S3)
537 Create a subclass of $P2 with name $S3 and return it in $P1.
547 Returns true if the class name passed in as a parameter is in the inheritance
548 hierarchy of the class, false otherwise.
558 Returns true if the class object can perform the requested method, false
565 $I1 = $P2.'does'($S3)
569 Returns true if the object in question implements the role, class, type,
570 or behavior passed in, false otherwise.
572 A class object only reports on interfaces of the class (i.e. roles composed
573 into the metaclass), while an instance object only reports on interfaces of
574 the instance (i.e. roles composed into the class).
580 $P1 = $P2.'inspect'()
581 $P1 = $P2.'inspect'($S3)
585 Return introspection information for the class.
589 =head3 Object PMC API
591 C<Object> PMCs are the actual objects, and hold all the per-object
594 An instance of the Object PMC has two core internal attributes, which
605 The object attribute store. This is simply an array of PMCs that provide
606 the values for the attributes. It may be a resizable PMC array to
607 provide lazy growth rather than allocating all needed memory for all
608 attributes. The attribute cache and lookup table in the class store the
609 indexes into this array, linking the attribute name and meta-information
610 with the storage position.
614 A list of the object's attributes is accessible from the class. The
615 attribute cache is the most straightforward way to retrieve a complete
616 list of attributes visible to the object, but the first time you
617 introspect for a complete list the class may have to calculate the list
618 by traversing the inheritance hierarchy.
620 Object PMCs have the "I am an object" flag set on them.
622 Object PMCs have no methods aside from those defined in their associated
623 class. They do have vtable functions providing access to certain low-level
624 information about the object, method call functionality, etc. See the
625 sections below on L<Objects> and L<Vtables>.
627 =head4 Object Vtable Entries
629 All Objects need the following vtable entries.
633 =item find_method(string *)
635 Returns the PMC for the named method. If no method of this name exists,
636 nor can be constructed, returns a Null PMC.
638 Note that for languages which support default fallback methods (such as
639 Perl 5's AUTOLOAD) this would be the place to return it if a normal
642 Since the method list and vtable override list are stored in the class
643 PMC, method finding is a lookup on the class object and not a lookup in
644 the namespace. Just adding a sub to a namespace will not automatically
645 make it a method of the class, you have to call add_method too.
647 An instance object reports on instance methods, not on class methods.
651 Returns true or false if the class passed in as a parameter is in the
652 inheritance hierarchy of the object.
656 Returns true or false if the object can perform the requested method.
657 (Class systems that implement default fallback methods should report
658 that they 'can' perform any method.)
660 An instance object only reports on instance methods, not on class methods.
664 Returns true or false to note whether the object in question implements the
667 An instance object only reports on interfaces of the instance (i.e. roles
668 composed into the class).
670 =item get_attr(STRING*)
672 Returns the attribute with the string name for the object.
674 =item get_attr_keyed(PMC*, STRING*)
676 Returns the attribute with the string name for the object. Lookup is on
677 the attribute store of a particular parent of the object's class,
678 identified by a classname, namespace, or key PMC.
680 =item set_attr(STRING*, PMC*)
682 Set the attribute with the string name for the object.
684 =item set_attr_keyed(PMC*, STRING*, PMC*)
686 Set the attribute with the string name for the object. The value is set
687 in the attribute store of a particular parent of the object's class,
688 identified by a classname, namespace, or key PMC.
692 Returns the class PMC for the object.
696 Create a clone of the object.
700 Return a data structure of all information relevant to introspection on
703 =item inspect_str(string *)
705 Return a PMC Hash, Array, String, Integer, or Number value of
706 introspection information corresponding to the requested string name
707 (such as 'parents'). This may be overridden to report information about
708 the internals of an object that aren't actually true (useful for
709 mocking). It can also be used for straight introspection capabilities
710 even when a particular object is using keyed access (to act like a hash
711 or array) or attribute access.
717 An instance of the Role PMC has five core attributes, which are:
727 A link to the role's associated namespace
731 An array PMC of composed roles
735 An array PMC of the methods defined in the role or composed into the
740 The role attribute hash, where each key is an attribute name and the
741 corresponding value is a hash of attribute characteristics, including
742 name, type, and the role they're associated with.
746 =head4 Role Vtable Entries
748 All Roles need the following vtable entries.
752 =item add_method(string *, method *)
754 Add a method to the role.
756 =item add_vtable_override(string *, vtable_sub *)
758 Add a vtable override to the role.
760 =item add_attribute(string *, key *)
762 Add an attribute to the role.
764 =item add_role(role *)
766 Add a role to the role.
768 =item find_method(string *)
770 Returns the PMC for the named method. If no method of this name exists,
771 nor can be constructed, returns a Null PMC.
773 A role object reports on class methods (methods of the metarole), not on
778 Returns true or false if the role can perform the requested method.
779 (Class systems that implement default fallback methods should report
780 that they 'can' perform any method.)
782 A role object reports on class methods (methods of the metarole), not on
787 Returns true or false to note whether the role in question implements the
790 A role object only reports on interfaces of the role (i.e. roles composed into
791 the metarole), it doesn't report on which interfaces will be added to an
792 object instantiated from a class that composes the role.
796 Create an (anonymous) clone of the role.
800 Return a data structure of all information relevant to introspection on
803 =item inspect_str(string *)
805 Return a PMC Hash, Array, String, Integer, or Number value of
806 introspection information corresponding to the requested string name
809 =item remove_attribute(string *)
811 Remove an attribute from the role.
813 =item remove_method(string *)
815 Remove a method from the role.
821 These methods are just syntactic sugar for the vtable functions.
829 $P1 = $P2.'name'( $S3 )
833 The accessor for the name attribute. With no argument, it simply returns
834 the current value for name. When passed an argument, it sets the name of
835 the role, and also sets the association with a namespace.
841 $P1 = $P2.'get_namespace'()
845 Retrieve the namespace object associated with the role.
851 $P1 = $P2.'attributes'()
855 An accessor for the attributes of the role. It returns the Hash of all
856 attributes, with a key of the attribute name, and a value of the
857 attribute's metadata (a Hash). The accessor is read-only.
863 $P1.'add_attribute'($S2)
864 $P1.'add_attribute'($S2, $S3)
865 $P1.'add_attribute'($S2, $P3)
869 Adds a single attribute to the role. It takes a simple string name, and
870 optionally, a simple string value or key specifying a type name. (A type
871 name just checks C<does>, and doesn't necessarily correspond to a class
876 =begin PIR_FRAGMENT_INVALID
878 $P1.'add_role'($P2, [named])
880 =end PIR_FRAGMENT_INVALID
882 Adds a single role to the role. It takes an instance of the Role PMC as a
883 required positional parameter, and the optional named parameters C<exclude>
884 and C<alias>; see L<Role Conflict Resolution> for more details.
894 An accessor for the roles composed into the role. It returns an Array of all
895 roles as PMC objects. If any roles that were composed into this one were
896 themselves made up of a composition of other roles, the roles they were made
897 up of will also be included in the value returned by this accessor. However,
898 no role will be mentioned more than once. The accessor is read-only.
904 $P1.'add_method'($S2, $P3)
908 Adds a method to the role. It takes a simple string name and a method
909 PMC. If the method already exists it will throw an exception.
911 =item add_vtable_override
915 $P1.'add_vtable_override'($S2, $P3)
919 Adds a vtable override to the role. It takes a simple string name and a
920 sub/method PMC. If the vtable override already exists it will throw an
927 $P1 = $P2.'methods'()
931 An accessor for the methods of the role. It returns a Hash of all methods,
932 with a key of the method name and a value of an invokable PMC. The list will
933 include methods added through composing other roles into this role. The
934 accessor is read-only.
940 $P1 = $P2.'inspect'()
941 $P1 = $P2.'inspect'($S3)
945 Return introspection information for the role.
949 =head4 Role Conflict Resolution
951 When a role is added to a class, we try to compose it right away, and
952 throw an exception on any conflicts that are detected. A conflict occurs
953 if two roles try to supply a method of the same name (but see the note
954 on multi-methods below). High level languages will provide varying
955 facilities to deal with this, and Parrot provides the primitives to
958 When declaring a composed class, you can optionally supply an array of
959 method names that will be defined by the class to resolve a conflict in
960 its roles. This is done using the named parameter C<resolve>. This
961 feature supports composition conflict resolution.
963 When adding a role to a class, you can optionally supply an array of
964 method names from the role to exclude from the composition process. This
965 is done using the named parameter C<exclude>. It is not an error to list
966 a method name in this array that the role does not have. This makes it
967 possible to implement languages that provide for explicit exclusions on
968 a role-by-role basis.
970 When adding a role to a class, you can optionally specify that specific
971 methods are to be aliased to different names within the class. This is
972 done with the optional C<alias> named parameter. The parameter takes
973 hash of strings, where the key is a method name in the role, and the
974 value is the name it will have in to the class. (This is also sometimes
975 used for conflict resolution.)
977 If you C<alias> a method, it won't automatically C<exclude> the original
978 name from the role. You can also explicitly C<exclude> the method name,
979 if you want a proper renaming of the method. A C<resolve> at the class
980 level will automatically C<exclude> all methods of that name from any
981 role composed into the class. You can C<alias> the method if you want to
982 call it from the composed class. (You might use this if you want the
983 resolving method to be able to call either of the conflicting methods
984 from two composed roles.)
988 The following ops are provided to deal with objects. Please note that
989 method calls are governed by Parrot's calling conventions, and as such
990 objects, method PMCs, return continuations, and parameters must be in
991 the right places, though some ops will put parameters where they need to
1000 $P1 = getattribute $P2, $S3
1001 $P1 = getattribute $P2, $P3, $S4
1005 Get the attribute with the fully qualified name $S3 from object $P2 and put it
1006 in $P1. To get an attribute for a parent class that has the same name as an
1007 attribute in the child class, pass an optional class object or namespace key
1008 $P3 for the parent class.
1010 If the attribute doesn't exist, it will throw an exception. If the attribute
1011 exists, but the value hasn't been set, it will return a null PMC.
1017 setattribute $P1, $S2, $P3
1018 setattribute $P1, $P2, $S3, $P4
1022 Set the attribute of object $P1 with the attribute name $S2 to $P3. To set an
1023 attribute for a parent class that has the same name as an attribute in the
1024 child class, pass an optional class object or namespace key $P2 for the parent
1027 If the attribute doesn't exist, it will throw an exception.
1033 callmethod $P1, $S1, $P2
1037 Call the method specified in the string name $S1 using $P1 as the invocant and
1038 using the continuation passed in $P2. If you need to create a new continuation
1039 use C<callmethodcc>.
1043 callmethod $P1, $P2, $P3
1047 Call the method specified in the Sub object $P2 using $P1 as the invocant and
1048 using the continuation passed in $P3. If you need to create a new continuation
1049 use C<callmethodcc>.
1056 callmethodcc $P1, $S1
1057 callmethodcc $P1, $P2
1061 Call the method specified in the string name $S1, or in the Sub object $P2,
1062 using $P1 as the invocant for method lookup and generate a new return
1065 Throws an exception for a non-existent method.
1067 =item callmethodsupercc [hypothetical, 2.0 or later]
1069 =begin PIR_FRAGMENT_TODO
1071 callmethodsupercc $P1, $S1
1072 callmethodsupercc $P1, $P2
1074 =end PIR_FRAGMENT_TODO
1076 Call the method specified in the string name $S1, or in the Sub object $P2,
1077 using $P1 as the invocant for method lookup and generate a new return
1078 continuation. This is a variant of C<callmethodcc> that skips over the
1079 current class when searching for the method, and only looks in the parent
1080 classes. PIR may provide some syntactic sugar for this.
1082 =item callmethodnextcc [hypothetical, 2.0 or later]
1084 =begin PIR_FRAGMENT_TODO
1086 callmethodnextcc $P1, $S1
1087 callmethodnextcc $P1, $P2
1089 =end PIR_FRAGMENT_TODO
1091 Call the method specified in the string name $S1, or in the Sub object $P2,
1092 using $P1 as the invocant for method lookup and generate a new return
1093 continuation. A variant of C<callmethodcc> that picks up an existing
1094 C<find_method> search where it left off for the current call. {{ Note: this
1095 depends on find_method being resumable, and on the context of a particular
1096 method including a pointer to the find_method call that found it. Neither may
1097 be feasible. }} PIR may provide some syntactic sugar for this.
1101 =begin PIR_FRAGMENT_INVALID
1104 $P1 = newclass $S2, $P3
1106 =end PIR_FRAGMENT_INVALID
1108 Create a new base class named $S2, and put the PMC for it in $P1. You may
1109 optionally pass a hash of initialization parameters for the class in $P3.
1117 $P1 = subclass $S2, $S3
1118 $P1 = subclass $P2, $S3
1119 $P1 = subclass $S2, $P3
1120 $P1 = subclass $P2, $P3
1124 Create a new class, named $S3, which has $P2 as its immediate parent. $P2 may
1125 be either another high-level class based on the Class PMC, or it may be a
1126 low-level PMC such as C<Integer> or C<ResizablePMCArray>.
1137 Retrieve a class object for the class identified by the string name in
1138 $S2, or by the PMC key or namespace object in $P2.
1140 A string name looks for the class in a namespace with that name nested in the
1141 currently selected namespace. Passing in a namespace object looks for the
1142 class in that namespace object. A key looks for the class in the namespace
1143 identified by the multilevel key relative to the currently selected HLL.
1145 If the class doesn't exist, it returns a null PMC.
1156 Lookup the type of the instance object in $P2. Return the string name if the
1157 destination is a string register or variable. If the destination is a PMC
1158 register or variable, return the class object for an instance of a high-level
1159 class, or the class proxy object for an instance of a low-level PMC.
1172 Create a new object from the class named by $S2 or $P2 (a string PMC,
1173 namespace key, or class object), and put the PMC for it in $P1. You may
1174 optionally pass a hash of initialization parameters for the class in $P3.
1184 Add class $P2 to the end of the list of immediate parents of class $P1.
1185 Adds any attributes of $P2 (and its parent classes) that aren't already
1192 removeparent $P1, $P2
1196 Remove class $P2 from the parent list of $P1. All parent classes of $P2
1197 which aren't parent classes of what remains of $P1's parent list are
1198 removed, as are their attributes.
1202 =begin PIR_FRAGMENT_INVALID
1204 addattribute $P1, $S2
1205 addattribute $P1, $S2, $S3
1206 addattribute $P1, $S2, $P3
1208 =end PIR_FRAGMENT_INVALID
1210 Add attribute $S2 to class or role $P1. This will add the attribute slot
1211 to all objects of class $P1, classes that inherit from class $P1, or
1212 classes that compose the role $P1, with a default value of C<Null>. It
1213 optionally takes a simple string value or key specifying a type of the
1216 =item removeattribute
1220 removeattribute $P1, $S2
1224 Remove the attribute $S2 from class or role $P1. This will not remove
1225 the attribute from any objects that have already been instantiated, but
1226 it will be absent from all future objects of class $P1, of classes that
1227 inherit from class $P1, or of classes that compose the role $P1.
1237 Add role $P2 to the end of the list of roles of class or role $P1. Adds
1238 any methods and attributes of $P2 that aren't already in $P1.
1245 $P1 = inspect $P2, $S3
1249 Return introspection information for the PMC. Without the optional string
1250 argument, return a data structure of all information relevant to
1251 introspection. With the optional string argument, return a PMC Hash, Array,
1252 String, Integer, or Number value with introspection information corresponding
1253 to the requested string name.
1257 =head3 PIR Class Definitions
1259 PIR provides some syntactic sugar for declaring classes.
1267 .sub custom_method :method
1273 Flags the code entity as a method.
1279 .sub get_integer :vtable
1285 Flags the code entity as a vtable override.
1289 :method and :vtable can be combined to indicate that a particular code entity
1290 is callable both as a method and as a vtable override.
1292 If the class object has not yet been created at the point when the PIR subs
1293 are compiled, the methods and vtable overrides are temporarily stored in the
1294 associated namespace.
1296 =head3 Vtable Overriding
1298 Classes can override vtable functions from PIR, allowing control over
1299 the low-level behavior of objects similar to PMCs defined in C. The
1300 vtable functions all take a single fixed argument list, and return at
1301 most a single value. Calling the overrides directly requires some
1302 knowledge of the way Parrot works, so higher-level languages will
1303 generally provide easier-to-use wrappers.
1305 To override a vtable function, either add the :vtable pragma to the
1306 declaration of the method, or pass a named parameter "vtable" into the
1307 C<add_method> method on a class or role.
1311 The following examples all assume we're working with basic Object objects
1314 =head3 Creating a new class
1316 To create a new class C<Foo> which has no parent classes:
1320 $P0 = newclass "Foo"
1324 =head3 Creating a new class with multiple parents
1326 To create a class C<Foo> with the parents C<A> and C<B>, the code would be:
1332 $P2 = subclass $P0, "Foo"
1337 =head3 Creating a new class with attributes
1339 Adding the attributes C<a> and C<b> to the new class C<Foo>:
1343 $P0 = newclass "Foo"
1344 addattribute $P0, "a"
1345 addattribute $P0, "b"
1349 =head3 Instantiating an object
1351 Assuming we want an object of class C<Foo>:
1357 FooClass = get_class "Foo"
1358 MyObject = FooClass.'new'()
1362 =head3 Calling a method on an object
1364 Calling the method C<Xyzzy> on an object, assuming the PDD03 calling
1365 conventions are respected:
1367 =begin PASM_FRAGMENT_INVALID
1374 =end PASM_FRAGMENT_INVALID
1376 Or, if a return continuation needs constructing:
1378 =begin PASM_FRAGMENT_INVALID
1380 callmethodcc "Xyzzy"
1385 =end PASM_FRAGMENT_INVALID
1387 Or, calling a method in PIR, where the calling conventions are handled
1396 =head3 Accessing attributes from within a class
1402 $P1 = getattribute $P0, "b"
1408 To get a new class, you can do a C<newclass>, which creates a new class
1409 with no parents besides Parrot's default super-ish parent class.
1411 To get a new child class, you have two potential options:
1415 =item Subclass the parent
1417 =item Create a new standalone class and add a parent
1421 Both ways work. It is, however, more efficient to use the first method, and
1422 just subclass the immediate parent class of your new class.
1424 When adding in extra parents in a multiple-inheritance scenario, subclass the
1425 first class in the immediate parent list then use the C<addparent> op to add
1426 in the rest of the immediate parents.
1428 =head2 Language Notes
1430 Notes on some of the OO-related needs of various languages.
1434 Ruby: Just like Smalltalk, everything is an object. Core Ruby classes
1435 (String, Array, Hash, Module, etc) might be implemented something like
1447 Ruby: Objectspace in Ruby allows the programmer to iterate through every live
1448 object in the system. There is some debate about how to make this play nice
1449 with different garbage collection schemes.
1453 A class is a collection of methods and attributes. It would be desirable, for
1454 those classes whose definition is fully known at compile time, to have a
1455 convenient way to have the class along with its attributes and methods stored
1456 into a PBC file rather than created at runtime. However, creation of new
1457 classes at runtime will be needed too.
1461 Ruby: Ruby has meta-classes. It would be nice if classes were objects in
1466 Attributes are instance data associated with a class (or role, however those
1467 are supported). They may not always be of a type specified by a PMC, though
1468 boxing/unboxing is of course an option.
1470 Perl 6: All attributes are opaque (not externally visible, even to any
1473 .Net: Attributes may be private (not externally visible), public (always
1474 externally visible), protected (only visible to subclasses) and internal
1475 (only visible inside the current assembly - the closest correspondence in
1476 Parrot is perhaps only visible inside the same PBC file). Additionally, it
1477 is allowable for a subclass to introduce an attribute of the same name as
1478 the a parent class has, and they both exist depending on what type an
1479 instance of the class is currently viewed as being (read: there is a
1480 difference between the type of the reference and the type of the value).
1482 Ruby: Attributes can be dynamically added and removed at runtime.
1486 Perl 6: Methods may be public (anyone can invoke them) or private (only
1487 invokable by the class they are defined in). Additionally, submethods are
1488 methods that do not get inherited.
1490 .Net: Like attributes, methods may be public, private, protected or internal.
1492 Ruby: has a method_missing that gets called when method resolution fails to
1493 find a method. Methods can be dynamically added and removed at runtime.
1497 A constructor is run when an object is instantiated.
1499 .Net: There may be many constructors for an object (provided they all have
1500 different signatures), and the correct one is called based upon the passed
1505 Perl 6: Multiple inheritance.
1507 .Net: Single inheritance.
1509 Ruby: Single inheritance but support for mixins of Ruby modules.
1513 An interface specifies a set of methods that must be implemented by a class
1514 that inherits (or implements) the interface, but does not provide any form of
1515 implementation for them.
1517 .Net: Interfaces are pretty much what was just describe above. XXX Need to
1518 check behavior of you implement two interfaces with methods of the same name.
1522 A role consists of a set of methods and attributes. It cannot be instantiated
1523 on its own, but must be composed into a class. When this happens its methods
1524 and attributes become of that classes methods and attributes. This may happen
1525 at compile time or runtime, however when a role is composed into a class at
1526 runtime then what really happens is that a new anonymous class is created with
1527 the role composed into it and then the namespace entry for the existing class
1528 is updated to refer to the new one. Note that this means classes must be
1529 garbage collectable, with all those referred to by a namespace or with objects
1530 of that class existing being marked live.
1532 Perl 6: Roles pretty much are a Perl 6 thing, so the definition above contains
1533 all that is needed. An open question is whether Parrot worry about collision
1534 detection? For compile time composition that's easy to punt to the compiler;
1535 for runtime composition, that's not so easy though.
1537 =head3 Introspection (aka Reflection)
1539 Perl 6: Reflection provides access to a list of methods that a class has, its
1540 parent classes and the roles it does, as well as the name of the class and its
1541 memory address. For methods, their name, signature, return type and whether
1542 the method is declared multi are available.
1544 .Net: Reflection provides access to a list of attributes and methods as well
1545 as the name of the class and its parent. The types of attributes and
1546 signatures of methods are also available.
1548 =head3 Inner Classes
1550 An inner class is essentially a class defined within a class. Therefore it has
1551 access to things private to its outer class.
1553 Perl 6: Inner classes are allowed, and may also be private.
1555 .Net: Inner classes are allowed and may be private, public, protected or
1560 Delegation is where a method call is "forwarded" to another class. Parrot may
1561 provide support for simple cases of it directly, or could just provide a "no
1562 method matched" fallback method that the compiler fills out to implement the
1565 Perl 6: Delegation support is highly flexible, even allowing a regex to match
1566 method names that should be delegated to a particular object.
1568 =head3 Prototype-based OO
1570 Prototype-based OO has no classes. All objects are cloned from existing
1571 objects and modified. Requires lightweight singleton creation, without
1572 needing a separate class for every instance object. (Self, JavaScript, and
1573 Io are examples of prototype-based 00.) An example from Io:
1575 Dog := Object clone # The Dog object is a clone of Object
1576 Dog tail := "long" # it has an attribute 'tail' with the value 'long'
1577 Dog bark := method("yap" print) # It has a method 'bark'
1578 Dog bark # call the method 'bark', printing 'yap'
1582 The following list a set of languages, then within each language what the
1583 Parrot term translates to.
1593 A Python attribute maps to a Parrot property
1603 What .NET calls an attribute Parrot calls a property
1607 What .NET calls a property we call an attribute
1611 =item Generic Terminology
1615 =item Instance Variable
1617 Instance Variables map to what we call attributes
1625 docs/pdds/pdd15_object_metamodel.png
1629 <img src="pdd15_object_metamodel.png">