Skip to content

Instantly share code, notes, and snippets.

@tadzik
Created July 1, 2012 19:19
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 tadzik/3029304 to your computer and use it in GitHub Desktop.
Save tadzik/3029304 to your computer and use it in GitHub Desktop.
Typed subs again
use Test;
plan 9;
sub typed_sub(*@types) {
return sub ($s) {
$s.signature.params == +@types
and @types ~~ $s.signature.params.map(*.type)
}
}
subset TwoArgSub of Sub where typed_sub(Any, Any);
my TwoArgSub $a;
lives_ok { $a = sub ($a, $b) { } }, 'lives 1';
dies_ok { $a = sub () { } };
dies_ok { $a = sub ($a) { } };
dies_ok { $a = sub ($a, $b, $c) { } };
subset TakesIntAndString of Sub where typed_sub(Int, Str);
my TakesIntAndString $b;
lives_ok { $b = sub (Int $a, Str $b) { } }, 'lives 2';
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