sub callwith

Documentation for sub callwith, assembled from the following types:

language documentation Functions

From Functions

(Functions) sub callwith

callwith calls the next matching candidate with arguments provided by users and returns that candidate's return value.

proto a(|) {*}
multi a(Any $x{
    say "Any $x";
    return 5;
multi a(Int $x{
    say "Int $x";
    my $res = callwith($x + 1);
    say "Back in Int with $res";
a 1;        # OUTPUT: «Int 1␤Any 2␤Back in Int with 5␤» 

Here, a 1 calls the most specific Int candidate first, and callwith re-dispatches to the less specific Any candidate. Note that although our parameter $x + 1 is an Int, still we call the next candidate in the chain.