在Perl 6中,有没有办法获得附加到特定多子候选者的Pod声明器块?

Enh*_*heh 9 code-documentation perl6 raku

Perl 6的具有实用的功能,允许人们得到任何波德声明符块附加到一个子程序(或类,角色等),使用该WHY方法:

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

say &myfunc.WHY;
Run Code Online (Sandbox Code Playgroud)

这显示:

Some enlightening words about myfunc.
Some more words about myfunc.
Run Code Online (Sandbox Code Playgroud)

不幸的是,当一个子程序有多个候选者时,不能只调用.WHY子程序名:

#|(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;
Run Code Online (Sandbox Code Playgroud)

结果:

No documentation available for type 'Sub'.
Perhaps it can be found at https://docs.perl6.org/type/Sub
Run Code Online (Sandbox Code Playgroud)

有没有办法获得附加到特定多子候选者的Pod声明器块?有没有办法为所有子程序的候选人这样做?

mr_*_*ron 8

你用candidates或查找多项cando.

在最初发布时,我找不到用于查找多个子签名的固定方法,但Christoph补救了这一点.

#| Initiate a specified spell normally
multi sub cast(Str $spell) {
  say "casting spell $spell";
}
#= (do not use for class 7 spells)

#| Cast a heavy rock etc in irritation
multi sub cast(Str $heavy-item, Int $n) {
  say "chucking $n heavy $heavy-item";
}

say "doc for cast spell";
say &cast.candidates[0].WHY;

say "doc for throwing rocks";
say &cast.candidates[1].WHY;

say "find doc for throwing things";
for &cast.candidates {
    if .signature ~~ :( Str, Int ) {
        say .WHY;
    }
}

# more advanced
say &cast.cando(\(Str, Int))>>.WHY; # thanks to Christoph
&cast.candidates.first: { .signature ~~ :(Str, Int) } andthen .WHY.say;
Run Code Online (Sandbox Code Playgroud)

OUTPUT:

doc for cast spell
Initiate a specified spell normally
(do not use for class 7 spells)
doc for throwing rocks
Cast a heavy rock etc in irritation
find doc for throwing things
Cast a heavy rock etc in irritation
... repeated for variants ...
Run Code Online (Sandbox Code Playgroud)

  • 你可以通过使用类似`&cast.cando(\(Str,Int))>>来更紧凑地编写你的最后一个例子.为什么 (3认同)

Chr*_*oph 7

通过候选人获取所有文件:

&myfunc.candidates>>.WHY
Run Code Online (Sandbox Code Playgroud)

通过cando获取最匹配候选人的文档:

&myfunc.cando(\(42)).first.WHY
Run Code Online (Sandbox Code Playgroud)