In Functions§

See primary documentation in context for sub nextcallee

Redispatch may be required to call a block that is not the current scope what provides nextwith and friends with the problem to referring to the wrong scope. Use nextcallee to capture the right candidate and call it at the desired time.

proto pick-winner(|) {*}
 
multi pick-winner (Int \s{
    my &nextone = nextcallee;
    Promise.in(π²).then: { nextone s }
}
multi pick-winner { say "Woot! $^w won" }
 
with pick-winner ^5 .pick -> \result {
    say "And the winner is...";
    await result;
}
 
# OUTPUT: 
# And the winner is... 
# Woot! 3 won 

The Int candidate takes the nextcallee and then fires up a Promise to be executed in parallel, after some timeout, and then returns. We can't use nextsame here, because it'd be trying to nextsame the Promise's block instead of our original routine.

Note that, despite its name, the nextcallee function is like:

  • callwith/nextwith, in that it takes parameters

  • callwith/callsame, in that it returns; the call to nextcallee returns a reference, and the call to the reference also returns (unlike nextwith/nextsame, which don't return)