什么是callwith和samewith应该做什么?

时间:2018-04-06 18:11:57

标签: perl6

callwithsamewith应该做什么?我认为callwith应该运行与当前子例程相同名称的子例程,但是使用我传递给它的参数。

来自文档:

  

callwith calls the next matching candidate with arguments provided by users and returns that candidate's return value.

对我来说,这听起来像是MAINMAIN($a,$b)callwith($x)所谓的MAIN($x)。{/ 1}}。

但它没有:

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)

然后samewith的这个定义听起来好像从两个samewith($x)(ig MAIN)内部调用MAIN($a, $b)会试图称之为两个再次MAIN("当前候选人",对吧?)。

  

samewith calls current candidate again with arguments provided by users and returns return value of the new instance of current candidate.

但它实际上称为单一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)

我是否看到了callwithsamewith的正确行为?如果是这样,我有什么误解?一旦我更好地理解了这个问题,我将很乐意更新文档。

我在CentOS 7.4.1708上使用Rakudo-Star-2018.01。

1 个答案:

答案 0 :(得分:4)

您看到的行为是正确的。

来自Zoffix的But here's my dispatch, so callwith Maybe

  

同样 - 在新的发送之后从头开始进行相同的呼叫,   链,带有这些新参数,然后回来

Zoffix还提到如果使用其中一个next____call_____函数(例如nextwithcallwith),则不会重新考虑任何先前被拒绝的候选人。

因此,在使用callwith之前,arity听起来已经修复了。 samewith解决了这个问题,因为它会重新启动" "从头开始" (感谢Brad Gilbert的评论,以及Zoffix的博客)。

如果您需要更改arity,请使用samewith或再次使用子例程名称(在这种情况下为MAIN)。

相关问题