Invocable code object
Callables can be stored in
&-sigiled containers, the default type constraint of such a container is
my = ; # Empty block needs a semicolonmy = -> ;my = sub () ;sub foo() ;my = ;
method CALL-ME(Callable : |arguments)
This method is required for postfix:«( )» and postfix:«.( )». It's what makes an object actually call-able and needs to be overloaded to let a given object act like a routine. If the object needs to be stored in a
&-sigiled container, is has to implement Callable.
does Callablemy = A;say a(); # OUTPUT: «called␤»
method assuming(Callable : |primers)
Callable that implements the same behaviour as the original, but has the values passed to .assuming already bound to the corresponding parameters.
my sub slow();# takes only one parameter and as such wont forward $nsub bench();say .assuming(10000000).; # OUTPUT: «(10000000 7.5508834)␤»
For a sub with arity greater than one, you can use
* for all of the positional parameters that are not "assumed".
sub first-and-last ( , )my = .assuming( *, 'Smith' );.( 'Joe' ); # OUTPUT: «Name is Joe Smith␤»
You can handle any combination of assumed and not assumed positional parameters:
sub longer-names ( , , , )my = .assuming( *, *, 'Public', * );.( 'Joe', 'Q.', 'Jr.'); # OUTPUT: «Name is Joe Q. Public Jr.␤»
Named parameters can be assumed as well:
sub foo.assuming(13, :42foo)(24, :72bar); # OUTPUT: «13 24 42 72␤»
# We use a Whatever star for the invocant:my = Str.^lookup('comb').assuming: *, /P \w+/;say comber 'Perl is awesome! Python is great! And PHP is OK too';# OUTPUT: «(Perl Python PHP)␤»my =.assuming: 'Perl 6';say learner :6months; # OUTPUT: «It took me 6 months to learn Perl 6␤»