Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing a routine's Capture from within

What's the syntax for accessing a subroutine Capture once it's called? self only works for objects, and &?ROUTINE refers to the static routine, not its state once called. So first, is it possible to access the routine's Capture from inside? If so, what's the syntax for accessing it? I've looked at the related Synopse but I can't find a way, if there's one.

like image 233
jjmerelo Avatar asked Mar 25 '18 21:03

jjmerelo


1 Answers

There's no way to do exactly what you're asking for. While conceptually arguments are passed by forming a Capture object holding them, which is then unpacked by a signature, for most calls no Capture ever really exists. With every operator in Perl 6 being a multi-dispatch subroutine call, the performance of calling is important, and the language design is such that there's plenty of room for implementations to cheat in order to achieve acceptable performance.

It is possible to explicitly ask for a Capture, however:

sub foo(|c ($a, $b)) { say c.perl; }
foo(1, 2);

This will capture the arguments into c and then unpack them also into $a and $b, enforcing that inner signature.

One might realize that things like callsame do indeed find a way to access the arguments to pass them on, even though no Capture appears in the signature. Their need to do so causes the compiler to opt any routine containing a callsame out of various optimizations, which would otherwise discard information needed to discover the arguments. This isn't ideal, and it's probable it will change in the future - most likely by finding a way to sneak a |SECRET-CAPTURE into the signature or similar.

like image 115
Jonathan Worthington Avatar answered Dec 31 '22 19:12

Jonathan Worthington