callwith 和 samewith 应该做什么? [英] What are callwith and samewith supposed to do?
问题描述
callwith
和 samewith
应该做什么?我认为 callwith
应该运行一个与当前子程序同名的子程序,但是使用我传递给它的参数.
来自文档:
<块引用>callwith
使用提供的参数调用下一个匹配的候选由用户并返回该候选人的返回值.
对我来说,这听起来像是在 callwith($x)<的二元
MAIN
子程序(即 MAIN($a,$b)
)中/code> 会调用 MAIN($x)
.
但它没有:
multi MAIN ($a, $b) {我的 $result = callwith("$a$b");说从 MAIN(\$a) 得到 $result";}多主 ($x) {说你好 $x";返回真;}
<块引用>
$ perl6 callwith.p6 foo bar在字符串上下文中使用 Any 类型的未初始化值 $result.方法 .^name、.perl、.gist 或 .say 可用于将其字符串化为有意义的内容.在 callwith.p6 第 3 行的 sub MAIN从 MAIN($a) 得到
那么 samewith
的这个定义听起来像是从二元 MAIN
内部调用 samewith($x)
(ig MAIN($a, $b)
) 会尝试再次调用二元MAIN
(当前候选者",对吧?)
samewith
使用提供的参数再次调用当前候选用户并返回当前新实例的返回值候选人.
但它实际上调用了一个元的MAIN
:
multi MAIN ($a, $b) {我的 $result = samewith("$a$b");说从 MAIN(\$a) 得到 $result";}多主 ($x) {说你好 $x";返回真;}
<块引用>
$ perl6 samewith.p6 foo bar你好 foobar从 MAIN($a) 得到真
那么我看到 callwith
和 samewith
的正确行为了吗?如果是这样,我有什么误解?一旦我更好地理解了这个问题,我很乐意更新文档.
我在 CentOS 7.4.1708 上使用 Rakudo-Star-2018.01.
您所看到的行为是正确的.
来自 但这是我的派遣,所以请致电也许 来自 Zoffix:
<块引用>samewith — 从头开始调用相同的调用,在新的调度之后链,带着这些新的参数,然后回来
Zoffix 还提到如果使用 next____
或 call_____
函数之一(例如 nextwith
或 callwith
),任何先前被拒绝的候选人将不会被重新考虑.
因此,在使用 callwith
之前,听起来签名*(或捕获)已经修复.samewith
解决了这个问题,因为它从头开始"重新调度"(感谢 Brad Gilbert 的评论和 Zoffix 的博客).
如果您需要更改使用的签名(或捕获),请使用 samewith
或再次使用子程序名称(在这种情况下为 MAIN
).
*我最初称此为arity",但签名"或捕获"是更好的术语(感谢 @jjmerelo 和 @Tinmarino 向我指出这些术语).
What are callwith
and samewith
supposed to do? I thought callwith
was supposed to run a subroutine of the same name of the current subroutine, but using the arguments I pass to it.
From the docs:
To me it sounds like inside the two-arity MAIN
subroutine (i.e. MAIN($a,$b)
) that callwith($x)
would call MAIN($x)
.
But it doesn't:
multi MAIN ($a, $b) {
my $result = callwith("$a$b");
say "Got $result from MAIN(\$a)";
}
multi MAIN ($x) {
say "Hello $x";
return True;
}
$ perl6 callwith.p6 foo bar Use of uninitialized value $result of type Any in string context. Methods .^name, .perl, .gist, or .say can be used to stringify it to something meaningful. in sub MAIN at callwith.p6 line 3 Got from MAIN($a)
Then this definition of samewith
makes it sound like calling samewith($x)
from inside of the two-arity MAIN
(i.g. MAIN($a, $b)
) would try to call the two-arity MAIN
again ("current candidate", right?).
But it actually calls the one-arity MAIN
:
multi MAIN ($a, $b) {
my $result = samewith("$a$b");
say "Got $result from MAIN(\$a)";
}
multi MAIN ($x) {
say "Hello $x";
return True;
}
$ perl6 samewith.p6 foo bar Hello foobar Got True from MAIN($a)
So am I seeing correct behavior by callwith
and samewith
? If so, what am I misunderstanding? I'll gladly update the documentation once I understand this issue better.
I'm using Rakudo-Star-2018.01 on CentOS 7.4.1708.
The behavior you are seeing is correct.
From But here's my dispatch, so callwith Maybe by Zoffix:
samewith — make the same call from scratch, following a new dispatch chain, with these new arguments, and come back
Zoffix also mentioned that if using one of the next____
or call_____
functions (e.g. nextwith
or callwith
), any previously rejected candidates will not be reconsidered.
So, it sounds like the signature* (or capture) is already fixed before using callwith
. samewith
gets around this because it "redispatches" "from scratch" (Thanks Brad Gilbert for your comments, and Zoffix for your blog).
If you need to change the signature used (or capture), use samewith
or just use the subroutine name again (MAIN
in this case).
*I originally called this "arity", but "signature" or "capture" are better terms (thanks @jjmerelo and @Tinmarino for pointing me to these terms).
这篇关于callwith 和 samewith 应该做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!