1 package Mouse::Meta::TypeConstraint;
2 use Mouse::Util qw(:meta); # enables strict and warnings
7 my %args = @_ == 1 ? %{$_[0]} : @_;
9 $args{name} = '__ANON__' if !defined $args{name};
12 %args = (%{$args{parent}}, %args);
13 # a child type must not inherit 'compiled_type_constraint'
14 # and 'hand_optimized_type_constraint' from the parent
15 delete $args{compiled_type_constraint};
16 delete $args{hand_optimized_type_constraint};
21 if($check = delete $args{optimized}) {
22 $args{hand_optimized_type_constraint} = $check;
23 $args{compiled_type_constraint} = $check;
25 elsif(my $param = $args{type_parameter}) {
26 my $generator = $args{constraint_generator}
27 || $class->throw_error("The $args{name} constraint cannot be used,"
28 . " because $param doesn't subtype from a parameterizable type");
29 # it must be 'constraint'
30 $check = $args{constraint} = $generator->($param);
33 $check = $args{constraint};
36 if(defined($check) && ref($check) ne 'CODE'){
38 "Constraint for $args{name} is not a CODE reference");
41 my $self = bless \%args, $class;
42 $self->compile_type_constraint()
43 if !$args{hand_optimized_type_constraint};
45 if($args{type_constraints}) {
46 $self->_compile_union_type_coercion();
51 sub create_child_type{
53 return ref($self)->new(
55 # and inherits other attributes from the parent
68 sub __is_parameterized;
70 sub _compiled_type_constraint;
71 sub _compiled_type_coercion;
73 sub compile_type_constraint;
76 sub _add_type_coercions { # ($self, @pairs)
79 my $coercions = ($self->{coercion_map} ||= []);
80 my %has = map{ $_->[0] => undef } @{$coercions};
82 for(my $i = 0; $i < @_; $i++){
84 my $action = $_[++$i];
86 if(exists $has{$from}){
87 $self->throw_error("A coercion action already exists for '$from'");
90 my $type = Mouse::Util::TypeConstraints::find_or_parse_type_constraint($from)
91 or $self->throw_error(
92 "Could not find the type constraint ($from) to coerce from");
94 push @{$coercions}, [ $type => $action ];
98 if(exists $self->{type_constraints}){ # union type
100 "Cannot add additional type coercions to Union types");
103 $self->_compile_type_coercion();
108 sub _compile_type_coercion {
111 my @coercions = @{$self->{coercion_map}};
113 $self->{_compiled_type_coercion} = sub {
115 foreach my $pair (@coercions) {
116 #my ($constraint, $converter) = @$pair;
117 if ($pair->[0]->check($thing)) {
119 return $pair->[1]->($thing);
127 sub _compile_union_type_coercion {
131 foreach my $type(@{$self->{type_constraints}}){
132 if($type->has_coercion){
133 push @coercions, $type;
137 $self->{_compiled_type_coercion} = sub {
139 foreach my $type(@coercions){
140 my $value = $type->coerce($thing);
141 return $value if $self->check($value);
152 my $coercion = $self->_compiled_type_coercion;
154 $self->throw_error("Cannot coerce without a type coercion");
157 return $_[0] if $self->check(@_);
159 return $coercion->(@_);
163 my ($self, $value) = @_;
164 if ( my $msg = $self->message ) {
166 return $msg->($value);
169 if(not defined $value) {
172 elsif( ref($value) && defined(&overload::StrVal) ) {
173 $value = overload::StrVal($value);
175 return "Validation failed for '$self' with value $value";
180 my($self, $other) = @_;
182 # ->is_a_type_of('__ANON__') is always false
183 return 0 if !ref($other) && $other eq '__ANON__';
185 (my $other_name = $other) =~ s/\s+//g;
187 return 1 if $self->name eq $other_name;
189 if(exists $self->{type_constraints}){ # union
190 foreach my $type(@{$self->{type_constraints}}) {
191 return 1 if $type->name eq $other_name;
195 for(my $p = $self->parent; defined $p; $p = $p->parent) {
196 return 1 if $p->name eq $other_name;
202 # See also Moose::Meta::TypeConstraint::Parameterizable
204 my($self, $param, $name) = @_;
207 require Mouse::Util::TypeConstraints;
208 $param = Mouse::Util::TypeConstraints::find_or_create_isa_type_constraint($param);
211 $name ||= sprintf '%s[%s]', $self->name, $param->name;
212 return Mouse::Meta::TypeConstraint->new(
215 type_parameter => $param,
220 my ($self, $value) = @_;
222 if(!$self->check($value)){
223 $self->throw_error($self->get_message($value));
228 sub _as_string { $_[0]->name } # overload ""
229 sub _identity { Scalar::Util::refaddr($_[0]) } # overload 0+
231 sub _unite { # overload infix:<|>
233 require Mouse::Util::TypeConstraints;
234 return Mouse::Util::TypeConstraints::find_or_parse_type_constraint(
244 Mouse::Meta::TypeConstraint - The Mouse Type Constraint metaclass
248 This document describes Mouse version 0.71
252 This class represents a type constraint, including built-in
253 type constraints, union type constraints, parameterizable/
254 parameterized type constraints, as well as custom type
261 =item C<< Mouse::Meta::TypeConstraint->new(%options) >>
263 =item C<< $constraint->name >>
265 =item C<< $constraint->parent >>
267 =item C<< $constraint->constraint >>
269 =item C<< $constraint->has_coercion >>
271 =item C<< $constraint->message >>
273 =item C<< $constraint->is_a_subtype_of($name or $object) >>
275 =item C<< $constraint->coerce($value) >>
277 =item C<< $constraint->check($value) >>
279 =item C<< $constraint->assert_valid($value) >>
281 =item C<< $constraint->get_message($value) >>
283 =item C<< $constraint->create_child_type(%options) >>
289 L<Moose::Meta::TypeConstraint>