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};
11 my $check = delete $args{optimized};
14 $args{hand_optimized_type_constraint} = $check;
15 $args{compiled_type_constraint} = $check;
18 $check = $args{constraint};
20 if(defined($check) && ref($check) ne 'CODE'){
22 "Constraint for $args{name} is not a CODE reference");
25 my $self = bless \%args, $class;
26 $self->compile_type_constraint()
27 if !$self->{hand_optimized_type_constraint};
29 $self->_compile_union_type_coercion() if $self->{type_constraints};
33 sub create_child_type{
35 return ref($self)->new(
36 # a child inherits its parent's attributes
39 # but does not inherit 'compiled_type_constraint'
40 # and 'hand_optimized_type_constraint'
41 compiled_type_constraint => undef,
42 hand_optimized_type_constraint => undef,
44 # and is given child-specific args, of course.
60 sub __is_parameterized;
62 sub _compiled_type_constraint;
63 sub _compiled_type_coercion;
65 sub compile_type_constraint;
68 sub _add_type_coercions { # ($self, @pairs)
71 my $coercions = ($self->{coercion_map} ||= []);
72 my %has = map{ $_->[0] => undef } @{$coercions};
74 for(my $i = 0; $i < @_; $i++){
76 my $action = $_[++$i];
78 if(exists $has{$from}){
79 $self->throw_error("A coercion action already exists for '$from'");
82 my $type = Mouse::Util::TypeConstraints::find_or_parse_type_constraint($from)
83 or $self->throw_error(
84 "Could not find the type constraint ($from) to coerce from");
86 push @{$coercions}, [ $type => $action ];
90 if(exists $self->{type_constraints}){ # union type
92 "Cannot add additional type coercions to Union types");
95 $self->_compile_type_coercion();
100 sub _compile_type_coercion {
103 my @coercions = @{$self->{coercion_map}};
105 $self->{_compiled_type_coercion} = sub {
107 foreach my $pair (@coercions) {
108 #my ($constraint, $converter) = @$pair;
109 if ($pair->[0]->check($thing)) {
111 return $pair->[1]->($thing);
119 sub _compile_union_type_coercion {
123 foreach my $type(@{$self->{type_constraints}}){
124 if($type->has_coercion){
125 push @coercions, $type;
129 $self->{_compiled_type_coercion} = sub {
131 foreach my $type(@coercions){
132 my $value = $type->coerce($thing);
133 return $value if $self->check($value);
144 my $coercion = $self->_compiled_type_coercion;
146 $self->throw_error("Cannot coerce without a type coercion");
149 return $_[0] if $self->check(@_);
151 return $coercion->(@_);
155 my ($self, $value) = @_;
156 if ( my $msg = $self->message ) {
158 return $msg->($value);
161 if(not defined $value) {
164 elsif( ref($value) && defined(&overload::StrVal) ) {
165 $value = overload::StrVal($value);
167 return "Validation failed for '$self' with value $value";
172 my($self, $other) = @_;
174 # ->is_a_type_of('__ANON__') is always false
175 return 0 if !ref($other) && $other eq '__ANON__';
177 (my $other_name = $other) =~ s/\s+//g;
179 return 1 if $self->name eq $other_name;
181 if(exists $self->{type_constraints}){ # union
182 foreach my $type(@{$self->{type_constraints}}) {
183 return 1 if $type->name eq $other_name;
187 for(my $p = $self->parent; defined $p; $p = $p->parent) {
188 return 1 if $p->name eq $other_name;
194 # See also Moose::Meta::TypeConstraint::Parameterizable
196 my($self, $param, $name) = @_;
199 require Mouse::Util::TypeConstraints;
200 $param = Mouse::Util::TypeConstraints::find_or_create_isa_type_constraint($param);
203 $name ||= sprintf '%s[%s]', $self->name, $param->name;
205 my $generator = $self->{constraint_generator}
206 || $self->throw_error("The $name constraint cannot be used,"
207 . " because $param doesn't subtype from a parameterizable type");
209 return Mouse::Meta::TypeConstraint->new(
212 type_parameter => $param,
213 constraint => $generator->($param), # must be 'constraint', not 'optimized'
218 my ($self, $value) = @_;
220 if(!$self->check($value)){
221 $self->throw_error($self->get_message($value));
226 sub _as_string { $_[0]->name } # overload ""
227 sub _identity { Scalar::Util::refaddr($_[0]) } # overload 0+
229 sub _unite { # overload infix:<|>
231 require Mouse::Util::TypeConstraints;
232 return Mouse::Util::TypeConstraints::find_or_parse_type_constraint(
242 Mouse::Meta::TypeConstraint - The Mouse Type Constraint metaclass
246 This document describes Mouse version 0.70
250 This class represents a type constraint, including built-in
251 type constraints, union type constraints, parameterizable/
252 parameterized type constraints, as well as custom type
259 =item C<< Mouse::Meta::TypeConstraint->new(%options) >>
261 =item C<< $constraint->name >>
263 =item C<< $constraint->parent >>
265 =item C<< $constraint->constraint >>
267 =item C<< $constraint->has_coercion >>
269 =item C<< $constraint->message >>
271 =item C<< $constraint->is_a_subtype_of($name or $object) >>
273 =item C<< $constraint->coerce($value) >>
275 =item C<< $constraint->check($value) >>
277 =item C<< $constraint->assert_valid($value) >>
279 =item C<< $constraint->get_message($value) >>
281 =item C<< $constraint->create_child_type(%options) >>
287 L<Moose::Meta::TypeConstraint>