Hello,
I'm trying to understand how nextsame works.

Apparently I started from the wrong assumptions: I thought that once the
first matched sub in the chain called nextsame, the arguments were matched
against the following subs regardless of the return value.
It seems that while the return value is not taken in account during the
match process, it is checked once the sub return its value and it generates
an error if the two candidates in the chain have different return types.

The documentation (
https://docs.raku.org/language/functions#index-entry-dispatch_nextsame)
reads:

nextsame calls the next matching candidate with the same arguments that
> were used for the current candidate and never returns.
>

and doesn't mention the return value.

proto test(Str $a, |) {*}
multi test($a, Str $b --> List) {
  nextsame if $b ~~ /\d+/;  # if the second string contains a number
  return $a, $b
}
multi test($a, Int() $b --> Int) { return $b } # coerces the second
argument to Int

say test('hello', 1);           # output: 1
say test('hello', 'goodbye');   # output: (hello goodbye)
say test('hello', '1');         # error:  Type check failed for return
value; expected List but got Int (1)

Here I expected that in the third call, after the first multi matched, the
nextsame triggered a match on the second multi, regardless of the return
value. Instead apparently the match is triggered, but then the return value
of the first multi (List) is expected from the second multi (which returns
an Int).

I don't know if this is the desired behavior; if so probably it deserves to
be documented.

-- 
Fernando Santagata

Reply via email to