I'm trying to address this issue, which was actually raised by this other stackoverflow question related to the different behavior of callwith
and samewith
. The latter seems to be clearly defined, however, it's not so clear with callwith
.
Check out this example:
proto how-many(|) {*}
multi sub how-many( Pair $a, Pair $b ) {
say "Int $a and $b";
return "There are $a and $b"
}
multi sub how-many( $a, $b ) {
say "Not int $a and $b";
my $calling = callwith( 1 => $a, 2 => $b );
return $calling;
}
say how-many( "little piggie","littler piggie" );
According to the documentation, callwith
should call the next matching candidate. However, this is the output:
Not int little piggie and littler piggie
(Any)
So it's calling the second version of how-many
, then calling a non-existing function (apparently) and returning Nil
, which is passed as Any
to the calling routine.
I have also tried with using different signatures, but that does not work either. The example in the documentation apparently indicates that it will work only if the variables belong to the same class hierarchy. Is that the case? It might not be, since changing the positional signature to Any $a, Any $b
does not work either, neither changing the order of declaration.
Above, changing
callwith
withsamewith
will obviously work, but I'm trying to understand howcallwith
works, not make the code above work.
Besides, it seems to be going down the class hierarchy, but not up. This example in roast, the Perl 6 test suite, works:
my $tracker = '';
multi d($x) { $tracker ~= 'Any' ~ $x };
multi d(Int $x) { $tracker ~= 'Int'; callwith($x+1); $tracker ~= 'Int' };
lives-ok { d(3) }, 'can call callwith inside a multi sub';
However, if we change it so that the we use callwith
from the bottom of the hierarchy like this:
my $tracker = '';
multi d($x) { $tracker ~= 'Any' ~ callwith( "called-with" => $x) };
multi d(Pair $x) { $tracker ~= "Pair $x" };
say d( 3 );
It fails with
Use of Nil in string context in sub d at rewrite-test-callwith.p6 line 6
Is this intended behavior?