在接收器上下文中(第13行)在表达式“ -1”中无用的“-”

时间:2019-03-14 11:50:23

标签: perl6

我正在尝试针对此代码引发异常的功能进行测试:

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)

我该如何解决?

2 个答案:

答案 0 :(得分:7)

foo形式的声明:

foo: ...

label,其中...是它标记的语句。

因此,您编写的语句与以下内容相同:

bisection(&f, -2, 0), -1;

sink context中的-1留在LTA中,因此出现错误消息。

(该消息有点What's the difference these two function calling conventions?,因为您的错误显然是您认为标签语法是调用语法的函数,而错误消息的确表明了错误-in sink context-{{ 1}}是没有帮助的其他详细信息,可能会使您感到困惑。)

另请参阅 his

答案 1 :(得分:0)

只是想补充一下那些可能在何时可以使用冒号后跟参数来调用例程的困惑的人-这是在调用方法时唯一可用的另一种格式:

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