我正在尝试为使用此代码抛出异常的函数进行测试:
use v6;
use Test;
plan *;
use lib "lib";
use Math::ConvergenceMethods;
sub f ($x) {return $x + 1;}
{
is-approx: bisection(&f, -2, 0), -1;
dies-ok: { bisection(&f, 3, 2) }, "Incorrect arguments";
}
done-testing;
当我运行它时它会返回此警告:
WARNINGS for /home/antonio/Code/perl6/Math-ConvergenceMethods/t/bisection.t:
Useless use of "-" in expression "-1" in sink context (line 13)
Useless use of constant string "Incorrect arguments" in sink context (lines 14, 14)
我该如何解决?
foo
在形式声明中:
foo: ...
是一个label,其中...
是它标注的声明。
所以你写的声明与以下内容相同:
bisection(&f, -2, 0), -1;
它在-1
留下sink context,因此出现错误信息。
(该消息有点LTA,因为你的错误显然是你认为标签语法是一个函数调用语法,而错误消息确实指示错误 - in sink context
- Useless use of "-"
是额外的细节,没有帮助,可能添加到你的困惑。)
另见What's the difference these two function calling conventions?。
只想添加那些可能会混淆的人,你可以使用冒号后跟参数来调用例程 - 它是调用方法时唯一可用的替代格式:
my @list = 1 , 2 , 3 ;
map( { $_ * 2 } , @list ); # "classic" fn call, works with or w/o brackets
@list.map: { $_ * 2 } # OK - calling the map method of @list, no brackets or
# comma (!) required.
map: { $_ * 2 } , @list ; # fugetaboutit