5

I'm trying to make tests for a function that throws an exception with this code:

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;

And it returns this warnings when I run it:

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)

How can I fix it?

Stefan Becker
  • 5,695
  • 9
  • 20
  • 30
Antonio Gamiz Delgado
  • 1,871
  • 1
  • 12
  • 33

2 Answers2

7

The foo in a statement of the form:

foo: ...

is a label where the ... is the statement that it labels.

So the statement you've written is the same as:

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

which leaves the -1 in sink context, hence the error message.

(The message is somewhat LTA because your mistake is clearly that you thought the label syntax was a function calling syntax and while the error message does indicate the error -- in sink context -- the Useless use of "-" is additional detail that doesn't help and probably added to your confusion.)

See also What's the difference these two function calling conventions?.

raiph
  • 31,607
  • 3
  • 62
  • 111
0

Just want to add for those who might be confused as to when you can call a routine using the colon followed by arguments - it's an alternative format exclusively available when calling methods:

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
Marty
  • 2,788
  • 11
  • 17