Search code examples
rakucode-documentation

In Perl 6, is there a way to get the Pod declarator block that is attached to a specific multi sub candidate?


Perl 6 has a cool feature which allows one to get any Pod declarator block that is attached to a subroutine (or class, role, etc.), using the WHY method:

#|(Some enlightening words about myfunc.)
sub myfunc (Int $i) { say "You provided an integer: $i"; };
#=(Some more words about myfunc.)

say &myfunc.WHY;

This displays:

Some enlightening words about myfunc.
Some more words about myfunc.

Unfortunately, when one has multiple candidates for a subroutine, one can't just invoke .WHY on the subroutine name:

#|(myfunc accepts an integer.)
multi myfunc (Int $i) { say "You provided an integer $i"; };
#|(myfunc accepts a string.)
multi myfunc (Str $s) { say "You provided a string $s"; };

say &myfunc.WHY;

The result:

No documentation available for type 'Sub'.
Perhaps it can be found at https://docs.perl6.org/type/Sub

Is there a way to get the Pod declarator block that is attached to a specific multi sub candidate? Is there a way to do so for all a subroutine's candidates?


Solution

  • Get all documentation via candidates:

    &myfunc.candidates>>.WHY
    

    Get documentation of narrowest matching candidate via cando:

    &myfunc.cando(\(42)).first.WHY