代码之家  ›  专栏  ›  技术社区  ›  Antonio Gamiz Delgado

在sink上下文(第13行)中表达式“-1”中无用地使用“-”

  •  0
  • Antonio Gamiz Delgado  · 技术社区  · 6 年前

    我正在尝试对一个函数进行测试,该函数会引发以下代码异常:

    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)
    

    我该怎么修?

    0 回复  |  直到 6 年前
        1
  •  7
  •   raiph    6 年前

    这个 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? .

        2
  •  0
  •   Marty    6 年前

    对于那些可能对何时可以使用冒号后跟参数调用例程感到困惑的人,我想补充一点——这是一种在调用方法时唯一可用的替代格式:

    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