Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
MOP: can, parents
  • Loading branch information
moritz committed Feb 16, 2014
1 parent 0d938ff commit 861e01f
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lib/objects.pod
Expand Up @@ -468,6 +468,24 @@ Introspection is the process of getting information about an object or class
at runtime. In Perl 6, all introspection goes through the meta object. The
standard C<ClassHOW> for class-based objects offers these facilities:
=head3 can
Given a method names, it returns a L<Parcel> of methods that are available
with this name.
class A { method x($a) {} };
class B is A { method x() {} };
say B.^can('x').elems; # 2
for B.^can('x') {
say .arity; # 1, 2
}
In this example, class C<B> has two possible methods available with name C<x>
(though a normal method call would only invoke the one installed in C<B>
directly). The one in C<B> has arity 1 (i.e. it expects one argument, the
invocant (C<self>)), and the one in C<A> expects 2 arguments (C<self> and
C<$a>).
=head3 methods
Returns a list of public methods available on the class (which includes
Expand Down Expand Up @@ -497,4 +515,20 @@ Returns the name of the class:
say 'a string'.^name; # Str
=head3 parents
Returns the list of parent classes. By default it stops at L<Cool>, L<Any> or
L<Mu>, which you can suppress by supplying the C<:all> adverb. With C<:tree>,
a nested list is returned.
class D { };
class C1 is D { };
class C2 is D { };
class B is C1 is C2 { };
class A is B { };
say A.^parents(:all).perl; # (B, C1, C2, D, Any, Mu)
say A.^parents(:all, :tree).perl;
# ([B, [C1, [D, [Any, [Mu]]]], [C2, [D, [Any, [Mu]]]]],)
=end pod

0 comments on commit 861e01f

Please sign in to comment.