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(@_, parent => $self);
64 sub __is_parameterized;
66 sub _compiled_type_constraint;
67 sub _compiled_type_coercion;
69 sub compile_type_constraint;
72 sub _add_type_coercions { # ($self, @pairs)
75 my $coercions = ($self->{coercion_map} ||= []);
76 my %has = map{ $_->[0] => undef } @{$coercions};
78 for(my $i = 0; $i < @_; $i++){
80 my $action = $_[++$i];
82 if(exists $has{$from}){
83 $self->throw_error("A coercion action already exists for '$from'");
86 my $type = Mouse::Util::TypeConstraints::find_or_parse_type_constraint($from)
87 or $self->throw_error(
88 "Could not find the type constraint ($from) to coerce from");
90 push @{$coercions}, [ $type => $action ];
94 if(exists $self->{type_constraints}){ # union type
96 "Cannot add additional type coercions to Union types");
99 $self->_compile_type_coercion();
104 sub _compile_type_coercion {
107 my @coercions = @{$self->{coercion_map}};
109 $self->{_compiled_type_coercion} = sub {
111 foreach my $pair (@coercions) {
112 #my ($constraint, $converter) = @$pair;
113 if ($pair->[0]->check($thing)) {
115 return $pair->[1]->($thing);
123 sub _compile_union_type_coercion {
127 foreach my $type(@{$self->{type_constraints}}){
128 if($type->has_coercion){
129 push @coercions, $type;
133 $self->{_compiled_type_coercion} = sub {
135 foreach my $type(@coercions){
136 my $value = $type->coerce($thing);
137 return $value if $self->check($value);
148 my $coercion = $self->_compiled_type_coercion;
150 $self->throw_error("Cannot coerce without a type coercion");
153 return $_[0] if $self->check(@_);
155 return $coercion->(@_);
159 my ($self, $value) = @_;
160 if ( my $msg = $self->message ) {
162 return $msg->($value);
165 if(not defined $value) {
168 elsif( ref($value) && defined(&overload::StrVal) ) {
169 $value = overload::StrVal($value);
171 return "Validation failed for '$self' with value $value";
176 my($self, $other) = @_;
178 # ->is_a_type_of('__ANON__') is always false
179 return 0 if !ref($other) && $other eq '__ANON__';
181 (my $other_name = $other) =~ s/\s+//g;
183 return 1 if $self->name eq $other_name;
185 if(exists $self->{type_constraints}){ # union
186 foreach my $type(@{$self->{type_constraints}}) {
187 return 1 if $type->name eq $other_name;
191 for(my $p = $self->parent; defined $p; $p = $p->parent) {
192 return 1 if $p->name eq $other_name;
198 # See also Moose::Meta::TypeConstraint::Parameterizable
200 my($self, $param, $name) = @_;
203 require Mouse::Util::TypeConstraints;
204 $param = Mouse::Util::TypeConstraints::find_or_create_isa_type_constraint($param);
207 $name ||= sprintf '%s[%s]', $self->name, $param->name;
208 return Mouse::Meta::TypeConstraint->new(
211 type_parameter => $param,
216 my ($self, $value) = @_;
218 if(!$self->check($value)){
219 $self->throw_error($self->get_message($value));
224 sub _as_string { $_[0]->name } # overload ""
225 sub _identity { Scalar::Util::refaddr($_[0]) } # overload 0+
227 sub _unite { # overload infix:<|>
229 require Mouse::Util::TypeConstraints;
230 return Mouse::Util::TypeConstraints::find_or_parse_type_constraint(
240 Mouse::Meta::TypeConstraint - The Mouse Type Constraint metaclass
244 This document describes Mouse version 0.71
248 This class represents a type constraint, including built-in
249 type constraints, union type constraints, parameterizable/
250 parameterized type constraints, as well as custom type
257 =item C<< Mouse::Meta::TypeConstraint->new(%options) >>
259 =item C<< $constraint->name >>
261 =item C<< $constraint->parent >>
263 =item C<< $constraint->constraint >>
265 =item C<< $constraint->has_coercion >>
267 =item C<< $constraint->message >>
269 =item C<< $constraint->is_a_subtype_of($name or $object) >>
271 =item C<< $constraint->coerce($value) >>
273 =item C<< $constraint->check($value) >>
275 =item C<< $constraint->assert_valid($value) >>
277 =item C<< $constraint->get_message($value) >>
279 =item C<< $constraint->create_child_type(%options) >>
285 L<Moose::Meta::TypeConstraint>