1 package MooseX::Types::Dependent;
5 use Moose::Util::TypeConstraints;
6 use MooseX::Meta::TypeConstraint::Dependent;
7 use MooseX::Types -declare => [qw(Depending)];
10 our $AUTHORITY = 'cpan:JJNAPIORK';
14 MooseX::Types::Dependent - L<MooseX::Types> constraints that depend on values.
18 TDB: Syntax to be determined. Canonical is:
24 shift->exists(shift) ? 0:1;
29 possible sugar options
32 shift->exists(shift) ? 0:1;
35 May have some ready to go, such as
46 ## using object for comparison
48 as Dependent[Int, CompareCmd, Int];
50 Please see the test cases for more examples.
54 The following is a list of terms used in this documentation.
56 =head2 Dependent Type Constraint
60 =head2 Constraining Type Constraint
62 =head2 Constraining Value
66 A dependent type is a type constraint whose validity is dependent on a second
67 value. You defined the dependent type constraint with a primary type constraint
68 (such as 'Int') a 'constraining' value type constraint (such as a Set object)
69 and a coderef which will compare the incoming value to be checked with a value
70 that conforms to the constraining type constraint. Typically there should be a
71 comparision operator between the check value and the constraining value
73 =head2 Subtyping a Dependent type constraints
75 TDB: Need discussion and examples.
79 TBD: Need discussion and example of coercions working for both the
80 constrainted and dependent type constraint.
84 Newer versions of L<MooseX::Types> support recursive type constraints. That is
85 you can include a type constraint as a contained type constraint of itself.
86 Recursion is support in both the dependent and constraining type constraint. For
91 =head1 TYPE CONSTRAINTS
93 This type library defines the following constraints.
95 =head2 Depending[$dependent_tc, $codref, $constraining_tc]
97 Create a subtype of $dependent_tc that is constrainted by a value that is a
98 valid $constraining_tc using $coderef. For example;
100 subtype GreaterThanInt,
104 my($constraining_value, $check_value) = @_;
105 return $constraining_value > $check_value ? 1:0;
110 This would create a type constraint that takes an integer and checks it against
111 a second integer, requiring that the check value is greater. For example:
113 GreaterThanInt->check(5,10); ## Fails, 5 is less than 10
114 GreaterThanInt->check('a',10); ## Fails, 'a' is not an Int.
115 GreaterThanInt->check(5,'b'); ## Fails, 'b' is not an Int either.
116 GreaterThanInt->check(10,5); ## Success, 10 is greater than 5.
120 Here are some additional example usage for structured types. All examples can
121 be found also in the 't/examples.t' test. Your contributions are also welcomed.
127 Moose::Util::TypeConstraints::get_type_constraint_registry->add_type_constraint(
128 MooseX::Meta::TypeConstraint::Dependent->new(
129 name => "MooseX::Types::Dependent::Depending" ,
130 parent => find_type_constraint('ArrayRef'),
131 constraint_generator=> sub {
132 my ($callback, $constraining_value, $check_value) = @_;
133 return $callback->($constraining_value, $check_value) ? 1:0;
140 The following modules or resources may be of interest.
142 L<Moose>, L<MooseX::Types>, L<Moose::Meta::TypeConstraint>,
143 L<MooseX::Meta::TypeConstraint::Dependent>
147 Here's a list of stuff I would be happy to get volunteers helping with:
153 Examples of useful code with both POD and ideally a test case to show it
160 John Napiorkowski, C<< <jjnapiork@cpan.org> >>
162 =head1 COPYRIGHT & LICENSE
164 This program is free software; you can redistribute it and/or modify
165 it under the same terms as Perl itself.