fix codetest failure - ASSERT_ARGS does not have a ; after and
[parrot.git] / docs / pdds / pdd15_objects.pod
blobb3ed639567fdeeaa34d408d74e43b4673d126234
1 # Copyright (C) 2001-2010, Parrot Foundation.
2 # $Id$
4 =head1 PDD 15: Objects and Classes
6 =head2 Abstract
8 This PDD describes the semantics of Parrot's object and class systems.
10 =head2 Version
12 $Revision$
14 =head2 Definitions
16 =head3 Object
18 An object is a value that incorporates both data and behavior
19 related to that data.
21 =head3 Class
23 A class defines a pattern of characteristics and behaviors from which
24 objects are constructed.
26 =head3 Attribute
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
35 the class).
37 =head3 Method
39 A method is a piece of code that you invoke by name through an object.
40 Methods implement the behaviour of an object.
42 =head3 Parent class
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.
47 =head3 Child class
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.
52 =head3 Role
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.
59 =head3 Delegate
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.
65 =head3 Property
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.
73 =head3 Interface
75 An interface is a role that only adds methods.
77 =head2 Description
79 =over 4
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
119 method calls)
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
127 =back
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
135 lifting.
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.)
147 =head3 Class PMC API
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
153 any other PMC class.
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
159 vtable functions.
161 An instance of the Class PMC has eleven core internal attributes, which
162 are:
164 =over 4
166 =item 1
168 The class name
170 =item 2
172 A link to the class's associated namespace
174 =item 3
176 A "have I been instantiated since I was last modified" flag
178 =item 4
180 An array PMC of the immediate parent classes
182 =item 5
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
187 inheritance)
189 =item 6
191 An array PMC of the composed roles (these are Role PMCs, not string names of
192 roles).
194 =item 7
196 A hash PMC of the methods defined in the class or composed into the
197 class
199 =item 8
201 A hash PMC of the overloaded PMC vtable entries for the class.
203 =item 9
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.
211 =item 10
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
219 store.
221 =item 11
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.
229 =back
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
238 used.
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
257 classes by name.
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.
273 =over 4
275 =item instantiate()
277 Instantiate a new object from the class. Set the instantiated flag on the
278 class.
280 =item clone()
282 Create an (anonymous) clone of the class. Unset the instantiated flag on the
283 new class.
285 =item name(string *)
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.
316 =item isa(pmc *)
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.
327 =item can(string *)
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.
335 =item does(class *)
337 Returns true or false to note whether the class in question implements the
338 interface passed in.
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.
344 =item inspect()
346 Return a data structure of all information relevant to introspection on
347 the class.
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.
371 =back
373 Parrot only supports mutating class metainformation for Class classes.
374 This restriction may be lifted at some point.
376 =head4 Class Methods
378 These methods are just syntactic sugar for the vtable functions.
380 =over 4
382 =item name
384 =begin PIR_FRAGMENT
386     $P1 = $P2.'name'( $S3 )
388 =end PIR_FRAGMENT
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.
395 =item get_namespace
397 =begin PIR_FRAGMENT
399     $P1 = $P2.'get_namespace'()
401 =end PIR_FRAGMENT
403 Retrieve the namespace object associated with the class.
405 =item new
407 =begin PIR_FRAGMENT
409     $P1 = $P2.'new'( 'myattrib' => "Foo" )
411 =end PIR_FRAGMENT
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.
417 =item add_attribute
419 =begin PIR_FRAGMENT
421   $P1.'add_attribute'($S2)
422   $P1.'add_attribute'($S2, $S3)
423   $P1.'add_attribute'($S2, $P3)
425 =end PIR_FRAGMENT
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
430 introspection.)
432 If the class has already been instantiated, adding a new attribute
433 throws an exception.
435 =item attributes
437 =begin PIR_FRAGMENT
439   $P1 = $P2.'attributes'()
441 =end PIR_FRAGMENT
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.
447 =item add_method
449 =begin PIR_FRAGMENT
451   $P1.'add_method'($S2, $P3)
453 =end PIR_FRAGMENT
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.
458 =item methods
460 =begin PIR_FRAGMENT
462   $P1 = $P2.'methods'()
464 =end PIR_FRAGMENT
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
473 =begin PIR_FRAGMENT
475   $P1.'add_vtable_override'($S2, $P3)
477 =end PIR_FRAGMENT
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).
484 =item add_parent
486 =begin PIR_FRAGMENT
488   $P1.'add_parent'($P3)
490 =end PIR_FRAGMENT
492 Adds a single parent to the class. It takes a Class PMC argument (the
493 parent to add).
495 =item parents
497 =begin PIR_FRAGMENT
499   $P1 = $P2.'parents'()
501 =end PIR_FRAGMENT
503 An accessor for the parents of the class. It returns an array of all
504 parents. The accessor is read-only.
506 =item roles
508 =begin PIR_FRAGMENT
510   $P1 = $P2.'roles'()
512 =end PIR_FRAGMENT
514 An accessor for the roles of the class. It returns an array of all
515 roles. The accessor is read-only.
517 =item add_role
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.
529 =item subclass
531 =begin PIR_FRAGMENT
533   $P1 = $P2.'subclass'($S3)
535 =end PIR_FRAGMENT
537 Create a subclass of $P2 with name $S3 and return it in $P1.
539 =item isa
541 =begin PIR_FRAGMENT
543   $I1 = $P2.'isa'($S3)
545 =end PIR_FRAGMENT
547 Returns true if the class name passed in as a parameter is in the inheritance
548 hierarchy of the class, false otherwise.
550 =item can
552 =begin PIR_FRAGMENT
554   $I1 = $P2.'can'($S3)
556 =end PIR_FRAGMENT
558 Returns true if the class object can perform the requested method, false
559 otherwise.
561 =item does
563 =begin PIR_FRAGMENT
565   $I1 = $P2.'does'($S3)
567 =end PIR_FRAGMENT
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).
576 =item inspect
578 =begin PIR_FRAGMENT
580   $P1 = $P2.'inspect'()
581   $P1 = $P2.'inspect'($S3)
583 =end PIR_FRAGMENT
585 Return introspection information for the class.
587 =back
589 =head3 Object PMC API
591 C<Object> PMCs are the actual objects, and hold all the per-object
592 instance data.
594 An instance of the Object PMC has two core internal attributes, which
595 are:
597 =over 4
599 =item 1
601 The class PMC
603 =item 2
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. 
612 =back
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.
631 =over 4
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
640 lookup fails.
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.
649 =item isa(class *)
651 Returns true or false if the class passed in as a parameter is in the
652 inheritance hierarchy of the object.
654 =item can(string *)
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.
662 =item does(class *)
664 Returns true or false to note whether the object in question implements the
665 interface passed in.
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.
690 =item get_class
692 Returns the class PMC for the object.
694 =item clone
696 Create a clone of the object.
698 =item inspect()
700 Return a data structure of all information relevant to introspection on
701 the object.
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.
713 =back
715 =head3 Role PMC API
717 An instance of the Role PMC has five core attributes, which are:
719 =over 4
721 =item 1
723 The role name
725 =item 2
727 A link to the role's associated namespace
729 =item 3
731 An array PMC of composed roles
733 =item 4
735 An array PMC of the methods defined in the role or composed into the
736 role
738 =item 5
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.
744 =back
746 =head4 Role Vtable Entries
748 All Roles need the following vtable entries.
750 =over 4
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
774 instance methods.
776 =item can(string *)
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
783 instance methods.
785 =item does(class *)
787 Returns true or false to note whether the role in question implements the
788 interface passed in.
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.
794 =item clone
796 Create an (anonymous) clone of the role.
798 =item inspect()
800 Return a data structure of all information relevant to introspection on
801 the role.
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
807 (such as 'parents').
809 =item remove_attribute(string *)
811 Remove an attribute from the role.
813 =item remove_method(string *)
815 Remove a method from the role.
817 =back
819 =head4 Role Methods
821 These methods are just syntactic sugar for the vtable functions.
823 =over 4
825 =item name
827 =begin PIR_FRAGMENT
829     $P1 = $P2.'name'( $S3 )
831 =end PIR_FRAGMENT
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.
837 =item get_namespace
839 =begin PIR_FRAGMENT
841     $P1 = $P2.'get_namespace'()
843 =end PIR_FRAGMENT
845 Retrieve the namespace object associated with the role.
847 =item attributes
849 =begin PIR_FRAGMENT
851   $P1 = $P2.'attributes'()
853 =end PIR_FRAGMENT
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.
859 =item add_attribute
861 =begin PIR_FRAGMENT
863   $P1.'add_attribute'($S2)
864   $P1.'add_attribute'($S2, $S3)
865   $P1.'add_attribute'($S2, $P3)
867 =end PIR_FRAGMENT
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
872 or role namespace.)
874 =item add_role
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.
886 =item roles
888 =begin PIR_FRAGMENT
890   $P1 = $P2.'roles'()
892 =end PIR_FRAGMENT
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.
900 =item add_method
902 =begin PIR_FRAGMENT
904   $P1.'add_method'($S2, $P3)
906 =end PIR_FRAGMENT
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
913 =begin PIR_FRAGMENT
915   $P1.'add_vtable_override'($S2, $P3)
917 =end PIR_FRAGMENT
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
921 exception.
923 =item methods
925 =begin PIR_FRAGMENT
927   $P1 = $P2.'methods'()
929 =end PIR_FRAGMENT
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.
936 =item inspect
938 =begin PIR_FRAGMENT
940   $P1 = $P2.'inspect'()
941   $P1 = $P2.'inspect'($S3)
943 =end PIR_FRAGMENT
945 Return introspection information for the role.
947 =back
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
956 implement them.
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.)
986 =head3 Opcodes
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
994 =over 4
996 =item getattribute
998 =begin PIR_FRAGMENT
1000   $P1 = getattribute $P2, $S3
1001   $P1 = getattribute $P2, $P3, $S4
1003 =end PIR_FRAGMENT
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.
1013 =item setattribute
1015 =begin PIR_FRAGMENT
1017   setattribute $P1, $S2, $P3
1018   setattribute $P1, $P2, $S3, $P4
1020 =end PIR_FRAGMENT
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
1025 class.
1027 If the attribute doesn't exist, it will throw an exception.
1029 =item callmethod
1031 =begin PIR_FRAGMENT
1033   callmethod $P1, $S1, $P2
1035 =end PIR_FRAGMENT
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>.
1041 =begin PIR_FRAGMENT
1043   callmethod $P1, $P2, $P3
1045 =end PIR_FRAGMENT
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>.
1052 =item callmethodcc
1054 =begin PIR_FRAGMENT
1056   callmethodcc $P1, $S1
1057   callmethodcc $P1, $P2
1059 =end PIR_FRAGMENT
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
1063 continuation.
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.
1099 =item newclass
1101 =begin PIR_FRAGMENT_INVALID
1103   $P1 = newclass $S2
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.
1111 =item subclass
1113 =begin PIR_FRAGMENT
1115   $P1 = subclass $S2
1116   $P1 = subclass $P2
1117   $P1 = subclass $S2, $S3
1118   $P1 = subclass $P2, $S3
1119   $P1 = subclass $S2, $P3
1120   $P1 = subclass $P2, $P3
1122 =end PIR_FRAGMENT
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>.
1128 =item get_class
1130 =begin PIR_FRAGMENT
1132   $P1 = get_class $S2
1133   $P1 = get_class $P2
1135 =end PIR_FRAGMENT
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.
1147 =item typeof
1149 =begin PIR_FRAGMENT
1151   $S1 = typeof $P2
1152   $P1 = typeof $P2
1154 =end PIR_FRAGMENT
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.
1161 =item new
1163 =begin PIR_FRAGMENT
1165   $P1 = new $S2
1166   $P1 = new $S2, $P3
1167   $P1 = new $P2
1168   $P1 = new $P2, $P3
1170 =end PIR_FRAGMENT
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.
1176 =item addparent
1178 =begin PIR_FRAGMENT
1180   addparent $P1, $P2
1182 =end PIR_FRAGMENT
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
1186 in $P1.
1188 =item removeparent
1190 =begin PIR_FRAGMENT
1192   removeparent $P1, $P2
1194 =end PIR_FRAGMENT
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.
1200 =item addattribute
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
1214 attribute.
1216 =item removeattribute
1218 =begin PIR_FRAGMENT
1220   removeattribute $P1, $S2
1222 =end PIR_FRAGMENT
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.
1229 =item addrole
1231 =begin PIR_FRAGMENT
1233   addrole $P1, $P2
1235 =end PIR_FRAGMENT
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.
1240 =item inspect
1242 =begin PIR_FRAGMENT
1244   $P1 = inspect $P2
1245   $P1 = inspect $P2, $S3
1247 =end PIR_FRAGMENT
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.
1255 =back
1257 =head3 PIR Class Definitions
1259 PIR provides some syntactic sugar for declaring classes.
1261 =over 4
1263 =item :method
1265 =begin PIR
1267   .sub custom_method :method
1268     # ...
1269   .end
1271 =end PIR
1273 Flags the code entity as a method.
1275 =item :vtable
1277 =begin PIR
1279   .sub get_integer :vtable
1280     # ...
1281   .end
1283 =end PIR
1285 Flags the code entity as a vtable override.
1287 =back
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.
1309 =head2 Examples
1311 The following examples all assume we're working with basic Object objects
1312 and Class classes.
1314 =head3 Creating a new class
1316 To create a new class C<Foo> which has no parent classes:
1318 =begin PIR_FRAGMENT
1320    $P0 = newclass "Foo"
1322 =end PIR_FRAGMENT
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:
1328 =begin PIR_FRAGMENT
1330    $P0 = get_class "A"
1331    $P1 = get_class "B"
1332    $P2 = subclass $P0, "Foo"
1333    addparent $P2, $P1
1335 =end PIR_FRAGMENT
1337 =head3 Creating a new class with attributes
1339 Adding the attributes C<a> and C<b> to the new class C<Foo>:
1341 =begin PIR_FRAGMENT
1343   $P0 = newclass "Foo"
1344   addattribute $P0, "a"
1345   addattribute $P0, "b"
1347 =end PIR_FRAGMENT
1349 =head3 Instantiating an object
1351 Assuming we want an object of class C<Foo>:
1353 =begin PIR_FRAGMENT
1355   .local pmc FooClass
1356   .local pmc MyObject
1357   FooClass = get_class "Foo"
1358   MyObject = FooClass.'new'()
1360 =end PIR_FRAGMENT
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
1369   callmethod "Xyzzy"
1371   set S0, "Xyzzy"
1372   callmethod
1374 =end PASM_FRAGMENT_INVALID
1376 Or, if a return continuation needs constructing:
1378 =begin PASM_FRAGMENT_INVALID
1380   callmethodcc "Xyzzy"
1382   set S0, "Xyzzy"
1383   callmethodcc
1385 =end PASM_FRAGMENT_INVALID
1387 Or, calling a method in PIR, where the calling conventions are handled
1388 automatically.
1390 =begin PIR_FRAGMENT
1392   $P0.'Xyzzy'($P1)
1394 =end PIR_FRAGMENT
1396 =head3 Accessing attributes from within a class
1398 With named access:
1400 =begin PIR_FRAGMENT
1402   $P1 = getattribute $P0, "b"
1404 =end PIR_FRAGMENT
1406 =head2 Explanations
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:
1413 =over 4
1415 =item Subclass the parent
1417 =item Create a new standalone class and add a parent
1419 =back
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.
1432 =head3 PMCs
1434 Ruby: Just like Smalltalk, everything is an object.  Core Ruby classes
1435 (String, Array, Hash, Module, etc) might be implemented something like
1436 this:
1438  ParrotClass
1439     |
1440  RubyClass   String
1441     |         |
1442      \      /
1443     RubyString
1445 =head3 Objectspace
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.
1451 =head3 Classes
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.
1459 =head3 Meta-classes
1461 Ruby: Ruby has meta-classes.  It would be nice if classes were objects in
1462 Parrot's OO model.
1464 =head3 Attributes
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
1471 subclasses).
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.
1484 =head3 Methods
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.
1495 =head3 Constructors
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
1501 parameters.
1503 =head3 Inheritance
1505 Perl 6: Multiple inheritance.
1507 .Net: Single inheritance.
1509 Ruby: Single inheritance but support for mixins of Ruby modules.
1511 =head3 Interfaces
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.
1520 =head3 Roles
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
1556 internal.
1558 =head3 Delegation
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
1563 delegation.
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'
1580 =head3 Translation
1582 The following list a set of languages, then within each language what the
1583 Parrot term translates to.
1585 =over 4
1587 =item Python
1589 =over 4
1591 =item Attribute
1593 A Python attribute maps to a Parrot property
1595 =back
1597 =item .NET
1599 =over 4
1601 =item Attribute
1603 What .NET calls an attribute Parrot calls a property
1605 =item Property
1607 What .NET calls a property we call an attribute
1609 =back
1611 =item Generic Terminology
1613 =over 4
1615 =item Instance Variable
1617 Instance Variables map to what we call attributes
1619 =back
1621 =back
1623 =head2 Attachments
1625   docs/pdds/pdd15_object_metamodel.png
1626   
1627 =begin html
1629 <img src="pdd15_object_metamodel.png">
1631 =end html
1633 =head2 References
1635 None.
1637 =cut
1639 __END__
1640 Local Variables:
1641   fill-column:78
1642 End: