Skip to content

Commit 861e01f

Browse files
committed
MOP: can, parents
1 parent 0d938ff commit 861e01f

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

lib/objects.pod

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,24 @@ Introspection is the process of getting information about an object or class
468468
at runtime. In Perl 6, all introspection goes through the meta object. The
469469
standard C<ClassHOW> for class-based objects offers these facilities:
470470
471+
=head3 can
472+
473+
Given a method names, it returns a L<Parcel> of methods that are available
474+
with this name.
475+
476+
class A { method x($a) {} };
477+
class B is A { method x() {} };
478+
say B.^can('x').elems; # 2
479+
for B.^can('x') {
480+
say .arity; # 1, 2
481+
}
482+
483+
In this example, class C<B> has two possible methods available with name C<x>
484+
(though a normal method call would only invoke the one installed in C<B>
485+
directly). The one in C<B> has arity 1 (i.e. it expects one argument, the
486+
invocant (C<self>)), and the one in C<A> expects 2 arguments (C<self> and
487+
C<$a>).
488+
471489
=head3 methods
472490
473491
Returns a list of public methods available on the class (which includes
@@ -497,4 +515,20 @@ Returns the name of the class:
497515
498516
say 'a string'.^name; # Str
499517
518+
=head3 parents
519+
520+
Returns the list of parent classes. By default it stops at L<Cool>, L<Any> or
521+
L<Mu>, which you can suppress by supplying the C<:all> adverb. With C<:tree>,
522+
a nested list is returned.
523+
524+
class D { };
525+
class C1 is D { };
526+
class C2 is D { };
527+
class B is C1 is C2 { };
528+
class A is B { };
529+
530+
say A.^parents(:all).perl; # (B, C1, C2, D, Any, Mu)
531+
say A.^parents(:all, :tree).perl;
532+
# ([B, [C1, [D, [Any, [Mu]]]], [C2, [D, [Any, [Mu]]]]],)
533+
500534
=end pod

0 commit comments

Comments
 (0)