Skip to content

Instantly share code, notes, and snippets.

@tadzik
Created July 1, 2012 17:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tadzik/3029085 to your computer and use it in GitHub Desktop.
Save tadzik/3029085 to your computer and use it in GitHub Desktop.
Typed subroutines
use Test;
plan 9;
subset TwoArgSub of Sub where {
.signature.params == 2
};
my TwoArgSub $a;
lives_ok { $a = sub ($a, $b) { } };
dies_ok { $a = sub () { } };
dies_ok { $a = sub ($a) { } };
dies_ok { $a = sub ($a, $b, $c) { } };
subset TakesIntAndString of Sub where {
.signature.params == 2
and .signature.params[0].type ~~ Int
and .signature.params[1].type ~~ Str
}
my TakesIntAndString $b;
lives_ok { $b = sub (Int $a, Str $b) { } };
dies_ok { $b = sub (Int $a, $b) { } };
dies_ok { $b = sub ($a, Str $b) { } };
dies_ok { $b = sub (Str $a, Int $b) { } };
dies_ok { $b = sub (Int $a, Str $b, $c) { } };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment