-
-
Save alabamenhu/c4b11676ac3d207956fbc5dbab835394 to your computer and use it in GitHub Desktop.
Namable trait for Raku
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
multi sub trait_mod:<is>(Sub \code, :namable!) { | |
my Int %params; | |
for code.signature.params.grep(*.positional)<> Z ^Inf -> ($param, $index) { | |
my $name = $param.name ~~ / [<[$%@]> \*?]? <( .*/; | |
die "Use of trait 'also-named' is not allowed when two parameters share a name, even if the sigils/twigils are different." | |
if %params{$name}:exists; | |
%params{$name} = $index; | |
} | |
code.wrap: anon sub (|c) { | |
my Mu @positional = c.list; | |
my Mu %named = c.hash; | |
for %named.keys -> $key { | |
with %params{$key} -> $index { | |
@positional[$index] = %named{$key}; | |
%named{$key}:delete; | |
} | |
} | |
callwith |@positional, |%named; | |
} | |
} | |
sub make-intstr(Str $s, Int $i) is namable { | |
IntStr.new: $i, $s; | |
} | |
make-intstr 'a', 2; | |
make-intstr :s<foo>, :3i; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment