X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMooseX%2FDependent%2FTypes.pm;h=b23e750b9fb64ccd3d6190a2e5ef63c43afd5c71;hb=88f58fbff42a62f89cac9407546e9e45e3288bac;hp=c08f822f62930200d7be2cda82848c797598074b;hpb=019d00e6528ae09ee74a7f8eb3b99fecebed02c2;p=gitmo%2FMooseX-Dependent.git diff --git a/lib/MooseX/Dependent/Types.pm b/lib/MooseX/Dependent/Types.pm index c08f822..b23e750 100644 --- a/lib/MooseX/Dependent/Types.pm +++ b/lib/MooseX/Dependent/Types.pm @@ -13,14 +13,41 @@ MooseX::Dependent::Types - L constraints that depend on values. Within your L declared library module: use MooseX::Dependent::Types qw(Dependent); + + subtype Set, + as class_type("Set::Scalar"); - subtype UniqueID, + subtype UniqueInt, as Dependent[Int, Set], where { my ($int, $set) = @_; - return $set->find($int) ? 0:1; + return !$set->has($int); }; + subtype PositiveSet, + as Set, + where { + my ($set) = @_; + return !grep {$_ <0 } $set->members; + }; + + subtype PositiveUniqueInt, + as UniqueInt[PositiveSet]; + + my $set = Set::Scalar->new(1,2,3); + + UniqueInt([$set])->check(100); ## Okay, 100 isn't in (1,2,3) + UniqueInt([$set])->check(-99); ## Okay, -99 isn't in (1,2,3) + UniqueInt([$set])->check(2); ## Not OK, 2 is in (1,2,3) + + PositiveUniqueInt([$set])->check(100); ## Okay, 100 isn't in (1,2,3) + PositiveUniqueInt([$set])->check(-99); ## Not OK, -99 not Positive Int + PositiveUniqueInt([$set])->check(2); ## Not OK, 2 is in (1,2,3) + + my $negative_set = Set::Scalar->new(-1,-2,-3); + + UniqueInt([$negative_set])->check(100); ## Throws exception + =head1 DESCRIPTION A L library for creating dependent types. A dependent type @@ -108,7 +135,7 @@ Example subtype with additional constraints: shift >= 0; }; -Or you could have done the following instead (example of re-paramterizing) +Or you could have done the following instead: ## Subtype of Int for positive numbers subtype PositiveInt,