@@ -468,6 +468,24 @@ Introspection is the process of getting information about an object or class
468
468
at runtime. In Perl 6, all introspection goes through the meta object. The
469
469
standard C < ClassHOW > for class-based objects offers these facilities:
470
470
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
+
471
489
= head3 methods
472
490
473
491
Returns a list of public methods available on the class (which includes
@@ -497,4 +515,20 @@ Returns the name of the class:
497
515
498
516
say 'a string'.^name; # Str
499
517
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
+
500
534
= end pod
0 commit comments