Fix type constraints to get $tc->type_parameter correctly
[gitmo/Mouse.git] / lib / Mouse / Meta / TypeConstraint.pm
1 package Mouse::Meta::TypeConstraint;
2 use Mouse::Util qw(:meta); # enables strict and warnings
3
4 sub new {
5     my $class = shift;
6     my %args  = @_ == 1 ? %{$_[0]} : @_;
7
8     $args{name} = '__ANON__' if !defined $args{name};
9
10     my $type_parameter;
11     if(defined $args{parent}) { # subtyping
12         %args = (%{$args{parent}}, %args);
13
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
18
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",
25                     );
26             }
27         }
28     }
29
30     my $check;
31
32     if($check = delete $args{optimized}) { # likely to be builtins
33         $args{hand_optimized_type_constraint} = $check;
34         $args{compiled_type_constraint}       = $check;
35     }
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");
42
43         my $parameterized_check = $generator->($type_parameter);
44         if(defined(my $my_check = $args{constraint})) {
45             $check = sub {
46                 return $parameterized_check->($_) && $my_check->($_);
47             };
48         }
49         else {
50             $check = $parameterized_check;
51         }
52         $args{constraint} = $check;
53     }
54     else { # common cases
55         $check = $args{constraint};
56     }
57
58     if(defined($check) && ref($check) ne 'CODE'){
59         $class->throw_error(
60             "Constraint for $args{name} is not a CODE reference");
61     }
62
63     my $self = bless \%args, $class;
64     $self->compile_type_constraint()
65         if !$args{hand_optimized_type_constraint};
66
67     if($args{type_constraints}) {
68         $self->_compile_union_type_coercion();
69     }
70     return $self;
71 }
72
73 sub create_child_type {
74     my $self = shift;
75     return ref($self)->new(@_, parent => $self);
76 }
77
78 sub name;
79 sub parent;
80 sub message;
81 sub has_coercion;
82
83 sub check;
84
85 sub type_parameter;
86 sub __is_parameterized;
87
88 sub _compiled_type_constraint;
89 sub _compiled_type_coercion;
90
91 sub compile_type_constraint;
92
93
94 sub _add_type_coercions { # ($self, @pairs)
95     my $self = shift;
96
97     if(exists $self->{type_constraints}){ # union type
98         $self->throw_error(
99             "Cannot add additional type coercions to Union types '$self'");
100     }
101
102     my $coercions = ($self->{coercion_map} ||= []);
103     my %has       = map{ $_->[0] => undef } @{$coercions};
104
105     for(my $i = 0; $i < @_; $i++){
106         my $from   = $_[  $i];
107         my $action = $_[++$i];
108
109         if(exists $has{$from}){
110             $self->throw_error("A coercion action already exists for '$from'");
111         }
112
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");
116
117         push @{$coercions}, [ $type => $action ];
118     }
119
120     $self->_compile_type_coercion();
121     return;
122 }
123
124 sub _compile_type_coercion {
125     my($self) = @_;
126
127     my @coercions = @{$self->{coercion_map}};
128
129     $self->{_compiled_type_coercion} = sub {
130        my($thing) = @_;
131        foreach my $pair (@coercions) {
132             #my ($constraint, $converter) = @$pair;
133             if ($pair->[0]->check($thing)) {
134               local $_ = $thing;
135               return $pair->[1]->($thing);
136             }
137        }
138        return $thing;
139     };
140     return;
141 }
142
143 sub _compile_union_type_coercion {
144     my($self) = @_;
145
146     my @coercions;
147     foreach my $type(@{$self->{type_constraints}}){
148         if($type->has_coercion){
149             push @coercions, $type;
150         }
151     }
152     if(@coercions){
153         $self->{_compiled_type_coercion} = sub {
154             my($thing) = @_;
155             foreach my $type(@coercions){
156                 my $value = $type->coerce($thing);
157                 return $value if $self->check($value);
158             }
159             return $thing;
160         };
161     }
162     return;
163 }
164
165 sub coerce {
166     my $self = shift;
167     return $_[0] if $self->check(@_);
168
169     my $coercion = $self->{_compiled_type_coercion}
170         or $self->throw_error("Cannot coerce without a type coercion");
171     return  $coercion->(@_);
172 }
173
174 sub get_message {
175     my ($self, $value) = @_;
176     if ( my $msg = $self->message ) {
177         local $_ = $value;
178         return $msg->($value);
179     }
180     else {
181         if(not defined $value) {
182             $value = 'undef';
183         }
184         elsif( ref($value) && defined(&overload::StrVal) ) {
185             $value = overload::StrVal($value);
186         }
187         return "Validation failed for '$self' with value $value";
188     }
189 }
190
191 sub is_a_type_of {
192     my($self, $other) = @_;
193
194     # ->is_a_type_of('__ANON__') is always false
195     return 0 if !ref($other) && $other eq '__ANON__';
196
197     (my $other_name = $other) =~ s/\s+//g;
198
199     return 1 if $self->name eq $other_name;
200
201     if(exists $self->{type_constraints}){ # union
202         foreach my $type(@{$self->{type_constraints}}) {
203             return 1 if $type->name eq $other_name;
204         }
205     }
206
207     for(my $p = $self->parent; defined $p; $p = $p->parent) {
208         return 1 if $p->name eq $other_name;
209     }
210
211     return 0;
212 }
213
214 # See also Moose::Meta::TypeConstraint::Parameterizable
215 sub parameterize {
216     my($self, $param, $name) = @_;
217
218     if(!ref $param){
219         require Mouse::Util::TypeConstraints;
220         $param = Mouse::Util::TypeConstraints::find_or_create_isa_type_constraint($param);
221     }
222
223     $name ||= sprintf '%s[%s]', $self->name, $param->name;
224     return Mouse::Meta::TypeConstraint->new(
225         name           => $name,
226         parent         => $self,
227         type_parameter => $param,
228     );
229 }
230
231 sub assert_valid {
232     my ($self, $value) = @_;
233
234     if(!$self->check($value)){
235         $self->throw_error($self->get_message($value));
236     }
237     return 1;
238 }
239
240 sub _as_string { $_[0]->name                  } # overload ""
241 sub _identity;                                  # overload 0+
242
243 sub _unite { # overload infix:<|>
244     my($lhs, $rhs) = @_;
245     require Mouse::Util::TypeConstraints;
246     return Mouse::Util::TypeConstraints::find_or_parse_type_constraint(
247        " $lhs | $rhs",
248     );
249 }
250
251 1;
252 __END__
253
254 =head1 NAME
255
256 Mouse::Meta::TypeConstraint - The Mouse Type Constraint metaclass
257
258 =head1 VERSION
259
260 This document describes Mouse version 0.81
261
262 =head1 DESCRIPTION
263
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
267 constraints
268
269 =head1 METHODS
270
271 =over
272
273 =item C<< Mouse::Meta::TypeConstraint->new(%options) >>
274
275 =item C<< $constraint->name >>
276
277 =item C<< $constraint->parent >>
278
279 =item C<< $constraint->constraint >>
280
281 =item C<< $constraint->has_coercion >>
282
283 =item C<< $constraint->message >>
284
285 =item C<< $constraint->is_a_type_of($name or $object) >>
286
287 =item C<< $constraint->coerce($value) >>
288
289 =item C<< $constraint->check($value) >>
290
291 =item C<< $constraint->assert_valid($value) >>
292
293 =item C<< $constraint->get_message($value) >>
294
295 =item C<< $constraint->create_child_type(%options) >>
296
297 =back
298
299 =head1 SEE ALSO
300
301 L<Moose::Meta::TypeConstraint>
302
303 =cut
304