Created
July 1, 2012 19:19
-
-
Save tadzik/3029304 to your computer and use it in GitHub Desktop.
Typed subs again
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
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