1 package MooseX::Types::Parameterizable;
6 $VERSION = eval $VERSION;
8 use Moose::Util::TypeConstraints;
9 use MooseX::Meta::TypeConstraint::Parameterizable;
10 use MooseX::Types -declare => [qw(Parameterizable)];
14 MooseX::Types::Parameterizable - Create your own Parameterizable Types.
18 Within your L<MooseX::Types> declared library module:
21 use MooseX::Types::Parameterizable qw(Parameterizable);
22 use MooseX::Types::Moose qw(Int );
23 use MooseX::Types -declare=>[qw(Set UniqueInt PositiveSet)];
26 as class_type("Set::Scalar");
29 as Parameterizable[Int, Set],
32 return !$set->has($int);
39 return !grep {$_ <0 } $set->members;
42 subtype PositiveUniqueInt,
43 as UniqueInt[PositiveSet];
45 my $set = Set::Scalar->new(1,2,3);
47 UniqueInt([$set])->check(100); ## Okay, 100 isn't in (1,2,3)
48 UniqueInt([$set])->check(-99); ## Okay, -99 isn't in (1,2,3)
49 UniqueInt([$set])->check(2); ## Not OK, 2 is in (1,2,3)
51 PositiveUniqueInt([$set])->check(100); ## Okay, 100 isn't in (1,2,3)
52 PositiveUniqueInt([$set])->check(-99); ## Not OK, -99 not Positive Int
53 PositiveUniqueInt([$set])->check(2); ## Not OK, 2 is in (1,2,3)
55 my $negative_set = Set::Scalar->new(-1,-2,-3);
57 UniqueInt([$negative_set])->check(100); ## Throws exception
61 A L<MooseX::Types> library for creating parameterizable types. A parameterizable type
62 constraint for all intents and uses is a subclass of a parent type, but adds a
63 secondary type parameter which is available to constraint callbacks (such as
64 inside the 'where' clause) or in the coercions.
66 This allows you to create a type that has additional runtime advice, such as a
67 set of numbers within which another number must be unique, or allowable ranges
68 for a integer, such as in:
71 as Dict[max=>Int, min=>Int],
74 return $range->{max} > $range->{min};
78 as Parameterizable[Int, Range],
80 my ($value, $range) = @_;
81 return ($value >= $range->{min} &&
82 $value <= $range->{max});
85 RangedInt([{min=>10,max=>100}])->check(50); ## OK
86 RangedInt([{min=>50, max=>75}])->check(99); ## Not OK, 99 exceeds max
88 This throws a hard Moose exception. You'll need to capture it in an eval or
89 related exception catching system (see L<TryCatch> or <Try::Tiny>.)
91 RangedInt([{min=>99, max=>10}])->check(10); ## Not OK, not a valid Range!
93 If you can't accept a hard exception here, you'll need to test the constraining
96 my $range = {min=>99, max=>10};
97 if(my $err = Range->validate($range)) {
100 RangedInt($range)->check(99);
103 Please note that for ArrayRef or HashRef parameterizable type constraints, as in the
104 example above, as a convenience we automatically ref the incoming type
105 parameters, so that the above could also be written as:
107 RangedInt([min=>10,max=>100])->check(50); ## OK
108 RangedInt([min=>50, max=>75])->check(99); ## Not OK, 99 exceeds max
109 RangedInt([min=>99, max=>10])->check(10); ## Exception, not a valid Range!
111 This is the preferred syntax, as it improve readability and adds to the
112 conciseness of your type constraint declarations. An exception wil be thrown if
113 your type parameters don't match the required reference type.
115 Also not that if you 'chain' parameterization results with a method call like:
117 TypeConstraint([$ob])->method;
119 You need to have the "(...)" around the ArrayRef in the Type Constraint
120 parameters. This seems to have something to do with the precendent level of
121 "->". Patches or thoughts welcomed. You only need to do this in the above
122 case which I imagine is not a very common case.
124 ==head2 Subtyping a Parameterizable type constraints
126 When subclassing a parameterizable type you must be careful to match either the
127 required type parameter type constraint, or if re-parameterizing, the new
128 type constraints are a subtype of the parent. For example:
131 as Parameterizable[Int, Range],
133 my ($value, $range) = @_;
134 return ($value >= $range->{min} &&
135 $value =< $range->{max});
138 Example subtype with additional constraints:
140 subtype PositiveRangedInt,
146 Or you could have done the following instead:
148 ## Subtype of Int for positive numbers
152 my ($value, $range) = @_;
156 ## subtype Range to re-parameterize Range with subtypes
157 subtype PositiveRange,
158 as Range[max=>PositiveInt, min=>PositiveInt];
160 ## create subtype via reparameterizing
161 subtype PositiveRangedInt,
162 as RangedInt[PositiveRange];
164 Notice how re-parameterizing the parameterizable type 'RangedInt' works slightly
165 differently from re-parameterizing 'PositiveRange' Although it initially takes
166 two type constraint values to declare a parameterizable type, should you wish to
167 later re-parameterize it, you only use a subtype of the second type parameter
168 (the parameterizable type constraint) since the first type constraint sets the parent
169 type for the parameterizable type. In other words, given the example above, a type
170 constraint of 'RangedInt' would have a parent of 'Int', not 'Parameterizable' and for
171 all intends and uses you could stick it wherever you'd need an Int.
176 ## re-parameterized subtypes of NameAge containing a Parameterizable Int
177 subtype NameBetween18and35Age,
180 PositiveRangedInt[min=>18,max=>35],
183 One caveat is that you can't stick an unparameterized parameterizable type inside a
184 structure, such as L<MooseX::Types::Structured> since that would require the
185 ability to convert a 'containing' type constraint into a parameterizable type, which
186 is a capacity we current don't have.
190 Parameterizable types have some limited support for coercions. Several things must
191 be kept in mind. The first is that the coercion targets the type constraint
192 which is being made parameterizable, Not the parameterizable type. So for example if you
193 create a Parameterizable type like:
195 subtype RequiredAgeInYears,
198 subtype PersonOverAge,
199 as Parameterizable[Person, RequiredAgeInYears]
201 my ($person, $required_years_old) = @_;
202 return $person->years_old > $required_years_old;
205 This would validate the following:
207 my $person = Person->new(age=>35);
208 PersonOverAge([18])->check($person);
210 You can then apply the following coercion
212 coerce PersonOverAge,
214 via {Person->new(%$_)},
216 via {Person->new(age=>$_)};
218 This coercion would then apply to all the following:
220 PersonOverAge([18])->check(30); ## via the Int coercion
221 PersonOverAge([18])->check({age=>50}); ## via the Dict coercion
223 However, you are not allowed to place coercions on parameterizable types that have
224 had their constraining value filled, nor subtypes of such. For example:
226 coerce PersonOverAge[18],
230 That would generate a hard exception. This is a limitation for now until I can
231 devise a smarter way to cache the generated type constraints. However, I doubt
232 it will be a significant limitation, since the general use case is supported.
234 Lastly, the constraining value is available in the coercion in much the same way
235 it is available to the constraint.
237 ## Create a type constraint where a Person must be in the set
239 as Parameterizable[Person, PersonSet],
241 my ($person, $person_set) = @_;
242 $person_set->find($person);
248 my ($hashref, $person_set) = @_;
249 return $person_set->create($hash_ref);
256 =head1 TYPE CONSTRAINTS
258 This type library defines the following constraints.
260 =head2 Parameterizable[ParentTypeConstraint, ParameterizableValueTypeConstraint]
262 Create a subtype of ParentTypeConstraint with a dependency on a value that can
263 pass the ParameterizableValueTypeConstraint. If ParameterizableValueTypeConstraint is empty
264 we default to the 'Any' type constraint (see L<Moose::Util::TypeConstraints>).
266 This creates a type constraint which must be further parameterized at later time
267 before it can be used to ->check or ->validate a value. Attempting to do so
268 will cause an exception.
272 Moose::Util::TypeConstraints::get_type_constraint_registry->add_type_constraint(
273 MooseX::Meta::TypeConstraint::Parameterizable->new(
274 name => 'MooseX::Types::Parameterizable::Parameterizable',
275 parent => find_type_constraint('Any'),
276 constraint => sub {1},
282 John Napiorkowski, C<< <jjnapiork@cpan.org> >>
284 =head1 COPYRIGHT & LICENSE
286 This program is free software; you can redistribute it and/or modify
287 it under the same terms as Perl itself.