For the first question, you can create dynamic subsystems by returning sub from another. To accept only a parameter Ampere
(where the software version is "Ampere"), use type capture in the function signature:
sub make-combiner(Any:U ::Type $, &combine-logic) {
return sub (Type $a, Type $b) {
return combine-logic($a, $b);
}
}
my &int-adder = make-combiner Int, {$^a + $^b};
say int-adder(1, 2);
my &list-adder = make-combiner List, {(|$^a, |$^b)};
say list-adder(<a b>, <c d>);
say list-adder(1, <c d>);
, sub
, sub
, , "sub". (. .)
, : ? is export
: https://docs.perl6.org/language/modules.html#is_export
, is export
. , . , MyModule.pm6:
unit module MyModule;
sub make-combiner(Any:U ::Type $, &combine-logic) {
anon sub combiner(Type $a, Type $b) {
return combine-logic($a, $b);
}
}
my Str $name = 'int';
my $type = Int;
my package EXPORT::DEFAULT {
OUR::{"&{$name}-eater"} := make-combiner $type, {$^a + $^b};
}
Perl 6:
perl6 -I. -MMyModule -e "say int-eater(4, 3);"
, 7. , anon sub
, "" . , .
, , postfix. , Precedence
, . .