1 package Mouse::Meta::TypeConstraint;
2 use Mouse::Util qw(:meta); # enables strict and warnings
6 my %args = @_ == 1 ? %{$_[0]} : @_;
8 $args{name} = '__ANON__' if !defined $args{name};
11 if(defined $args{parent}) { # subtyping
12 %args = (%{$args{parent}}, %args);
14 # a child type must not inherit 'compiled_type_constraint'
15 # and 'hand_optimized_type_constraint' from the parent
16 delete $args{compiled_type_constraint}; # don't inherit it
17 delete $args{hand_optimized_type_constraint}; # don't inherit it
19 $type_parameter = $args{type_parameter};
20 if(defined(my $parent_tp = $args{parent}{type_parameter})) {
21 if($parent_tp != $type_parameter) {
22 $type_parameter->is_a_type_of($parent_tp)
23 or $class->throw_error(
24 "$type_parameter is not a subtype of $parent_tp",
32 if($check = delete $args{optimized}) { # likely to be builtins
33 $args{hand_optimized_type_constraint} = $check;
34 $args{compiled_type_constraint} = $check;
36 elsif(defined $type_parameter) { # parameterizing
37 my $generator = $args{constraint_generator}
38 || $class->throw_error(
39 "The $args{name} constraint cannot be used,"
40 . " because $type_parameter doesn't subtype"
41 . " from a parameterizable type");
43 my $parameterized_check = $generator->($type_parameter);
44 if(defined(my $my_check = $args{constraint})) {
46 return $parameterized_check->($_) && $my_check->($_);
50 $check = $parameterized_check;
52 $args{constraint} = $check;
55 $check = $args{constraint};
58 if(defined($check) && ref($check) ne 'CODE'){
60 "Constraint for $args{name} is not a CODE reference");
63 my $self = bless \%args, $class;
64 $self->compile_type_constraint()
65 if !$args{hand_optimized_type_constraint};
67 if($args{type_constraints}) {
68 $self->_compile_union_type_coercion();
73 sub create_child_type {
75 return ref($self)->new(@_, parent => $self);
86 sub __is_parameterized;
88 sub _compiled_type_constraint;
89 sub _compiled_type_coercion;
91 sub compile_type_constraint;
94 sub _add_type_coercions { # ($self, @pairs)
97 if(exists $self->{type_constraints}){ # union type
99 "Cannot add additional type coercions to Union types '$self'");
102 my $coercions = ($self->{coercion_map} ||= []);
103 my %has = map{ $_->[0] => undef } @{$coercions};
105 for(my $i = 0; $i < @_; $i++){
107 my $action = $_[++$i];
109 if(exists $has{$from}){
110 $self->throw_error("A coercion action already exists for '$from'");
113 my $type = Mouse::Util::TypeConstraints::find_or_parse_type_constraint($from)
114 or $self->throw_error(
115 "Could not find the type constraint ($from) to coerce from");
117 push @{$coercions}, [ $type => $action ];
120 $self->_compile_type_coercion();
124 sub _compile_type_coercion {
127 my @coercions = @{$self->{coercion_map}};
129 $self->{_compiled_type_coercion} = sub {
131 foreach my $pair (@coercions) {
132 #my ($constraint, $converter) = @$pair;
133 if ($pair->[0]->check($thing)) {
135 return $pair->[1]->($thing);
143 sub _compile_union_type_coercion {
147 foreach my $type(@{$self->{type_constraints}}){
148 if($type->has_coercion){
149 push @coercions, $type;
153 $self->{_compiled_type_coercion} = sub {
155 foreach my $type(@coercions){
156 my $value = $type->coerce($thing);
157 return $value if $self->check($value);
167 return $_[0] if $self->check(@_);
169 my $coercion = $self->{_compiled_type_coercion}
170 or $self->throw_error("Cannot coerce without a type coercion");
171 return $coercion->(@_);
175 my ($self, $value) = @_;
176 if ( my $msg = $self->message ) {
178 return $msg->($value);
181 if(not defined $value) {
184 elsif( ref($value) && defined(&overload::StrVal) ) {
185 $value = overload::StrVal($value);
187 return "Validation failed for '$self' with value $value";
192 my($self, $other) = @_;
194 # ->is_a_type_of('__ANON__') is always false
195 return 0 if !ref($other) && $other eq '__ANON__';
197 (my $other_name = $other) =~ s/\s+//g;
199 return 1 if $self->name eq $other_name;
201 if(exists $self->{type_constraints}){ # union
202 foreach my $type(@{$self->{type_constraints}}) {
203 return 1 if $type->name eq $other_name;
207 for(my $p = $self->parent; defined $p; $p = $p->parent) {
208 return 1 if $p->name eq $other_name;
214 # See also Moose::Meta::TypeConstraint::Parameterizable
216 my($self, $param, $name) = @_;
219 require Mouse::Util::TypeConstraints;
220 $param = Mouse::Util::TypeConstraints::find_or_create_isa_type_constraint($param);
223 $name ||= sprintf '%s[%s]', $self->name, $param->name;
224 return Mouse::Meta::TypeConstraint->new(
227 type_parameter => $param,
232 my ($self, $value) = @_;
234 if(!$self->check($value)){
235 $self->throw_error($self->get_message($value));
240 sub _as_string { $_[0]->name } # overload ""
241 sub _identity; # overload 0+
243 sub _unite { # overload infix:<|>
245 require Mouse::Util::TypeConstraints;
246 return Mouse::Util::TypeConstraints::find_or_parse_type_constraint(
256 Mouse::Meta::TypeConstraint - The Mouse Type Constraint metaclass
260 This document describes Mouse version 0.81
264 This class represents a type constraint, including built-in
265 type constraints, union type constraints, parameterizable/
266 parameterized type constraints, as well as custom type
273 =item C<< Mouse::Meta::TypeConstraint->new(%options) >>
275 =item C<< $constraint->name >>
277 =item C<< $constraint->parent >>
279 =item C<< $constraint->constraint >>
281 =item C<< $constraint->has_coercion >>
283 =item C<< $constraint->message >>
285 =item C<< $constraint->is_a_type_of($name or $object) >>
287 =item C<< $constraint->coerce($value) >>
289 =item C<< $constraint->check($value) >>
291 =item C<< $constraint->assert_valid($value) >>
293 =item C<< $constraint->get_message($value) >>
295 =item C<< $constraint->create_child_type(%options) >>
301 L<Moose::Meta::TypeConstraint>