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};
10 if(defined $args{parent}) {
11 %args = (%{$args{parent}}, %args);
12 # a child type must not inherit 'compiled_type_constraint'
13 # and 'hand_optimized_type_constraint' from the parent
14 delete $args{compiled_type_constraint};
15 delete $args{hand_optimized_type_constraint};
16 if(defined(my $parent_tp = $args{parent}{type_parameter})) {
17 delete $args{type_parameter} if $parent_tp == $args{type_parameter};
23 if($check = delete $args{optimized}) {
24 $args{hand_optimized_type_constraint} = $check;
25 $args{compiled_type_constraint} = $check;
27 elsif(my $param = $args{type_parameter}) {
28 my $generator = $args{constraint_generator}
29 || $class->throw_error("The $args{name} constraint cannot be used,"
30 . " because $param doesn't subtype from a parameterizable type");
31 # it must be 'constraint'
32 $check = $args{constraint} = $generator->($param);
35 $check = $args{constraint};
38 if(defined($check) && ref($check) ne 'CODE'){
40 "Constraint for $args{name} is not a CODE reference");
43 my $self = bless \%args, $class;
44 $self->compile_type_constraint()
45 if !$args{hand_optimized_type_constraint};
47 if($args{type_constraints}) {
48 $self->_compile_union_type_coercion();
53 sub create_child_type {
55 return ref($self)->new(@_, parent => $self);
66 sub __is_parameterized;
68 sub _compiled_type_constraint;
69 sub _compiled_type_coercion;
71 sub compile_type_constraint;
74 sub _add_type_coercions { # ($self, @pairs)
77 if(exists $self->{type_constraints}){ # union type
79 "Cannot add additional type coercions to Union types '$self'");
82 my $coercions = ($self->{coercion_map} ||= []);
83 my %has = map{ $_->[0] => undef } @{$coercions};
85 for(my $i = 0; $i < @_; $i++){
87 my $action = $_[++$i];
89 if(exists $has{$from}){
90 $self->throw_error("A coercion action already exists for '$from'");
93 my $type = Mouse::Util::TypeConstraints::find_or_parse_type_constraint($from)
94 or $self->throw_error(
95 "Could not find the type constraint ($from) to coerce from");
97 push @{$coercions}, [ $type => $action ];
100 $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);
147 return $_[0] if $self->check(@_);
149 my $coercion = $self->{_compiled_type_coercion}
150 or $self->throw_error("Cannot coerce without a type coercion");
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;
204 return Mouse::Meta::TypeConstraint->new(
207 type_parameter => $param,
212 my ($self, $value) = @_;
214 if(!$self->check($value)){
215 $self->throw_error($self->get_message($value));
220 sub _as_string { $_[0]->name } # overload ""
221 sub _identity; # overload 0+
223 sub _unite { # overload infix:<|>
225 require Mouse::Util::TypeConstraints;
226 return Mouse::Util::TypeConstraints::find_or_parse_type_constraint(
236 Mouse::Meta::TypeConstraint - The Mouse Type Constraint metaclass
240 This document describes Mouse version 0.80
244 This class represents a type constraint, including built-in
245 type constraints, union type constraints, parameterizable/
246 parameterized type constraints, as well as custom type
253 =item C<< Mouse::Meta::TypeConstraint->new(%options) >>
255 =item C<< $constraint->name >>
257 =item C<< $constraint->parent >>
259 =item C<< $constraint->constraint >>
261 =item C<< $constraint->has_coercion >>
263 =item C<< $constraint->message >>
265 =item C<< $constraint->is_a_subtype_of($name or $object) >>
267 =item C<< $constraint->coerce($value) >>
269 =item C<< $constraint->check($value) >>
271 =item C<< $constraint->assert_valid($value) >>
273 =item C<< $constraint->get_message($value) >>
275 =item C<< $constraint->create_child_type(%options) >>
281 L<Moose::Meta::TypeConstraint>