在 Perl 6 中,有没有办法获取附加到特定多子候选者的 Pod 声明符块?
In Perl 6, is there a way to get the Pod declarator block that is attached to a specific multi sub candidate?
Perl 6 有一个很酷的特性,它允许你得到任何 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;
这显示:
Some enlightening words about myfunc.
Some more words about myfunc.
不幸的是,当一个子程序有多个候选者时,不能仅在子程序名称上调用 .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;
结果:
No documentation available for type 'Sub'.
Perhaps it can be found at https://docs.perl6.org/type/Sub
有没有办法获取附加到特定多子候选者的 Pod 声明符块?有没有办法对子程序的所有候选人都这样做?
这有点间接,但是...
您可以将每个 multi myfunc
存储在一个变量中并对该变量调用 WHY
,但仍然像以前一样调用 myfunc
:
#!/bin/env perl6
#|(myfunc accepts an integer.)
my $func_int = multi myfunc (Int $i) { say "You provided an integer $i"; }
#=(More about Int version of myfunc)
#|(myfunc accepts a string.)
my $func_string = multi myfunc (Str $s) { say "You provided a string $s"; }
#=(More about Str version of myfunc)
myfunc(10); # myfunc works as normal
say $func_int.WHY; # show POD declarator block
say ''; # Blank line to separate output into two groups
myfunc("bar");
say $func_string.WHY;
导致此输出:
You provided an integer 10
myfunc accepts an integer.
More about Int version of myfunc
You provided a string bar
myfunc accepts a string.
More about Str version of myfunc
这是在 CentOS 6.7 上使用 Rakudo Star 2018.01。
您使用 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;
输出:
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 ...
这并没有真正回答您的问题,而是试图解释为什么在 multi
上使用 WHY
不起作用;主要是因为它指向multi
的proto
#|(my-multi-func accepts either an integer or a string)
proto my-multi-func (|) {*}
#|(myfunc accepts an integer.)
multi my-multi-func (Int $i) { say "You provided an integer $i"; };
#|(myfunc accepts a string.)
multi my-multi-func (Str $s) { say "You provided a string $s"; };
say "my-multi-func is a {&my-multi-func.perl} and does {&my-multi-func.WHY}";
我在此处附上 {&my-multi-func.perl}
,因为这是给我的提示。如果你不定义 proto
,它 returns
my-multi-func is a sub my-multi-func (;; Mu | is raw) { #`(Sub|59650976) ... }
,即定义的 multi
中的 none,因此 proto
。当然,如果您想访问候选人的那些特定定义,@Christopher Bottoms 的回答是完美的。
通过candidates获取所有文档:
&myfunc.candidates>>.WHY
通过cando获取最窄匹配候选的文档:
&myfunc.cando(\(42)).first.WHY
Perl 6 有一个很酷的特性,它允许你得到任何 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;
这显示:
Some enlightening words about myfunc.
Some more words about myfunc.
不幸的是,当一个子程序有多个候选者时,不能仅在子程序名称上调用 .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;
结果:
No documentation available for type 'Sub'.
Perhaps it can be found at https://docs.perl6.org/type/Sub
有没有办法获取附加到特定多子候选者的 Pod 声明符块?有没有办法对子程序的所有候选人都这样做?
这有点间接,但是...
您可以将每个 multi myfunc
存储在一个变量中并对该变量调用 WHY
,但仍然像以前一样调用 myfunc
:
#!/bin/env perl6
#|(myfunc accepts an integer.)
my $func_int = multi myfunc (Int $i) { say "You provided an integer $i"; }
#=(More about Int version of myfunc)
#|(myfunc accepts a string.)
my $func_string = multi myfunc (Str $s) { say "You provided a string $s"; }
#=(More about Str version of myfunc)
myfunc(10); # myfunc works as normal
say $func_int.WHY; # show POD declarator block
say ''; # Blank line to separate output into two groups
myfunc("bar");
say $func_string.WHY;
导致此输出:
You provided an integer 10 myfunc accepts an integer. More about Int version of myfunc You provided a string bar myfunc accepts a string. More about Str version of myfunc
这是在 CentOS 6.7 上使用 Rakudo Star 2018.01。
您使用 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;
输出:
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 ...
这并没有真正回答您的问题,而是试图解释为什么在 multi
上使用 WHY
不起作用;主要是因为它指向multi
proto
#|(my-multi-func accepts either an integer or a string)
proto my-multi-func (|) {*}
#|(myfunc accepts an integer.)
multi my-multi-func (Int $i) { say "You provided an integer $i"; };
#|(myfunc accepts a string.)
multi my-multi-func (Str $s) { say "You provided a string $s"; };
say "my-multi-func is a {&my-multi-func.perl} and does {&my-multi-func.WHY}";
我在此处附上 {&my-multi-func.perl}
,因为这是给我的提示。如果你不定义 proto
,它 returns
my-multi-func is a sub my-multi-func (;; Mu | is raw) { #`(Sub|59650976) ... }
,即定义的 multi
中的 none,因此 proto
。当然,如果您想访问候选人的那些特定定义,@Christopher Bottoms 的回答是完美的。
通过candidates获取所有文档:
&myfunc.candidates>>.WHY
通过cando获取最窄匹配候选的文档:
&myfunc.cando(\(42)).first.WHY