Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 255d1c1946ee467bc7901f9864727288b6dcbb9c @tadzik committed Jul 1, 2012
Showing with 85 additions and 0 deletions.
  1. +7 −0 META.info
  2. +20 −0 README
  3. +35 −0 lib/Typed/Subroutines.pm
  4. +23 −0 t/01-basic.t
@@ -0,0 +1,7 @@
+{
+ "name" : "Typed::Subroutines",
+ "version" : "*",
+ "description" : "Better type checking for first-class functions",
+ "depends" : [],
+ "source-url" : "git://github.com/tadzik/Typed-Subroutines.git"
+}
20 README
@@ -0,0 +1,20 @@
+SYNOPSIS
+
+ use Typed::Subroutines;
+ subset TwoArgSub of Sub where typed_sub(Any, Any);
+ subset TakesIntAndString of Sub where typed_sub(Int, Str);
+
+ my TwoArgSub $a;
+ my TakesIntAndString $b;
+
+ $a = sub ($a, $b) { ... }; # lives
+ $a = sub ($a) { ... }; # dies
+
+ $b = sub (Int $a, Str $b) { ... }; # lives
+ $b = sub (Int $a, $b) { ... }; # dies
+
+DESCRIPTION
+
+Typed::Subroutines let you specify subroutine types verifying the parameter list so you can have better type checking for first-class functions.
+
+I'll write more docs when I'm less tired.
@@ -0,0 +1,35 @@
+module Typed::Subroutines;
+
+sub typed_sub(*@types) is export {
+ return sub ($s) {
+ $s.signature.params == +@types
+ and @types ~~ $s.signature.params.map(*.type)
+ }
+}
+
+=begin pod
+
+=head1 SYNOPSIS
+
+ use Typed::Subroutines;
+ subset TwoArgSub of Sub where typed_sub(Any, Any);
+ subset TakesIntAndString of Sub where typed_sub(Int, Str);
+
+ my TwoArgSub $a;
+ my TakesIntAndString $b;
+
+ $a = sub ($a, $b) { ... }; # lives
+ $a = sub ($a) { ... }; # dies
+
+ $b = sub (Int $a, Str $b) { ... }; # lives
+ $b = sub (Int $a, $b) { ... }; # dies
+
+=head1 DESCRIPTION
+
+Typed::Subroutines let you specify subroutine types verifying the
+parameter list so you can have better type checking for
+first-class functions.
+
+I'll write more docs when I'm less tired.
+
+=end pod
@@ -0,0 +1,23 @@
+use Test;
+plan 9; # from outer space
+
+use Typed::Subroutines;
+
+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) { } };

0 comments on commit 255d1c1

Please sign in to comment.