Skip to content

Instantly share code, notes, and snippets.

@colomon
Created December 3, 2011 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colomon/1425858 to your computer and use it in GitHub Desktop.
Save colomon/1425858 to your computer and use it in GitHub Desktop.
sub trait_mod:<is>(Routine $r, :$symmetric!) {
sub gen(::T2, ::T1) {
(sub (T1 $a, T2 $b) { say "woah"; $r($b, $a) }).instantiate_generic(nqp::hash('T1', T1, 'T2', T2));
};
my @params = $r.signature.params;
die "symmetric only works on binary functions" unless @params == 2;
gen(@params[0].type, @params[1].type);
}
multi sub foo(Int $a, Str $b) is symmetric {
say $b x $a;
}
foo(10, "Hello"); # works
foo("Hello", 10); # results in
# Calling 'foo' will never work with argument types (str, int) (line 18)
# Expected any of:
# :(Int $a, Str $b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment