Fix type constraints to get $tc->type_parameter correctly
[gitmo/Mouse.git] / lib / Mouse / Meta / TypeConstraint.pm
CommitLineData
684db121 1package Mouse::Meta::TypeConstraint;
bc69ee88 2use Mouse::Util qw(:meta); # enables strict and warnings
9c85e9dc 3
684db121 4sub new {
184f8f53 5 my $class = shift;
6 my %args = @_ == 1 ? %{$_[0]} : @_;
f5ee065f 7
8 $args{name} = '__ANON__' if !defined $args{name};
684db121 9
eed39dff 10 my $type_parameter;
11 if(defined $args{parent}) { # subtyping
5a592ad7 12 %args = (%{$args{parent}}, %args);
eed39dff 13
5a592ad7 14 # a child type must not inherit 'compiled_type_constraint'
15 # and 'hand_optimized_type_constraint' from the parent
eed39dff 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};
7e030e45 20 if(defined(my $parent_tp = $args{parent}{type_parameter})) {
eed39dff 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 }
7e030e45 27 }
5a592ad7 28 }
29
30 my $check;
3b89ea91 31
eed39dff 32 if($check = delete $args{optimized}) { # likely to be builtins
3b89ea91 33 $args{hand_optimized_type_constraint} = $check;
34 $args{compiled_type_constraint} = $check;
35 }
eed39dff 36 elsif(defined $type_parameter) { # parameterizing
5a592ad7 37 my $generator = $args{constraint_generator}
eed39dff 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;
5a592ad7 53 }
eed39dff 54 else { # common cases
5a592ad7 55 $check = $args{constraint};
56 }
f5ee065f 57
f5ee065f 58 if(defined($check) && ref($check) ne 'CODE'){
3a29a080 59 $class->throw_error(
60 "Constraint for $args{name} is not a CODE reference");
f5ee065f 61 }
62
63 my $self = bless \%args, $class;
3a29a080 64 $self->compile_type_constraint()
5a592ad7 65 if !$args{hand_optimized_type_constraint};
f5ee065f 66
5a592ad7 67 if($args{type_constraints}) {
68 $self->_compile_union_type_coercion();
69 }
f5ee065f 70 return $self;
71}
72
c5940bae 73sub create_child_type {
f5ee065f 74 my $self = shift;
c5940bae 75 return ref($self)->new(@_, parent => $self);
684db121 76}
77
4c99ea95 78sub name;
79sub parent;
80sub message;
81sub has_coercion;
2b82d75d 82
c7576321 83sub check;
84
2b82d75d 85sub type_parameter;
86sub __is_parameterized;
87
4c99ea95 88sub _compiled_type_constraint;
89sub _compiled_type_coercion;
90
91sub compile_type_constraint;
92
c7576321 93
3a29a080 94sub _add_type_coercions { # ($self, @pairs)
ffbbf459 95 my $self = shift;
96
4b58033d 97 if(exists $self->{type_constraints}){ # union type
98 $self->throw_error(
99 "Cannot add additional type coercions to Union types '$self'");
100 }
101
cc131889 102 my $coercions = ($self->{coercion_map} ||= []);
ffbbf459 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}){
c7576321 110 $self->throw_error("A coercion action already exists for '$from'");
ffbbf459 111 }
112
113 my $type = Mouse::Util::TypeConstraints::find_or_parse_type_constraint($from)
3a29a080 114 or $self->throw_error(
115 "Could not find the type constraint ($from) to coerce from");
ffbbf459 116
117 push @{$coercions}, [ $type => $action ];
118 }
119
4b58033d 120 $self->_compile_type_coercion();
536e3c33 121 return;
122}
123
124sub _compile_type_coercion {
125 my($self) = @_;
126
cc131889 127 my @coercions = @{$self->{coercion_map}};
536e3c33 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
143sub _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){
ffbbf459 153 $self->{_compiled_type_coercion} = sub {
536e3c33 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;
ffbbf459 160 };
161 }
162 return;
163}
164
ffbbf459 165sub coerce {
166 my $self = shift;
c7576321 167 return $_[0] if $self->check(@_);
ffbbf459 168
4b58033d 169 my $coercion = $self->{_compiled_type_coercion}
170 or $self->throw_error("Cannot coerce without a type coercion");
de0d4152 171 return $coercion->(@_);
feb0e21b 172}
173
174sub get_message {
175 my ($self, $value) = @_;
176 if ( my $msg = $self->message ) {
177 local $_ = $value;
178 return $msg->($value);
179 }
180 else {
6e647cac 181 if(not defined $value) {
182 $value = 'undef';
183 }
184 elsif( ref($value) && defined(&overload::StrVal) ) {
185 $value = overload::StrVal($value);
186 }
537873b0 187 return "Validation failed for '$self' with value $value";
feb0e21b 188 }
189}
190
4b58033d 191sub is_a_type_of {
feb0e21b 192 my($self, $other) = @_;
193
194 # ->is_a_type_of('__ANON__') is always false
ca352580 195 return 0 if !ref($other) && $other eq '__ANON__';
feb0e21b 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
3a29a080 202 foreach my $type(@{$self->{type_constraints}}) {
feb0e21b 203 return 1 if $type->name eq $other_name;
204 }
205 }
206
3a29a080 207 for(my $p = $self->parent; defined $p; $p = $p->parent) {
208 return 1 if $p->name eq $other_name;
feb0e21b 209 }
210
211 return 0;
212}
213
b4d791ba 214# See also Moose::Meta::TypeConstraint::Parameterizable
7e030e45 215sub parameterize {
b4d791ba 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;
b4d791ba 224 return Mouse::Meta::TypeConstraint->new(
fc83f4cf 225 name => $name,
226 parent => $self,
227 type_parameter => $param,
b4d791ba 228 );
229}
feb0e21b 230
78508064 231sub assert_valid {
232 my ($self, $value) = @_;
233
c7576321 234 if(!$self->check($value)){
235 $self->throw_error($self->get_message($value));
78508064 236 }
237 return 1;
238}
239
6e647cac 240sub _as_string { $_[0]->name } # overload ""
f6c81f00 241sub _identity; # overload 0+
6e647cac 242
243sub _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
684db121 2511;
252__END__
253
254=head1 NAME
255
1820fffe 256Mouse::Meta::TypeConstraint - The Mouse Type Constraint metaclass
684db121 257
a25ca8d6 258=head1 VERSION
259
ce1cb320 260This document describes Mouse version 0.81
a25ca8d6 261
684db121 262=head1 DESCRIPTION
263
3a29a080 264This class represents a type constraint, including built-in
265type constraints, union type constraints, parameterizable/
266parameterized type constraints, as well as custom type
267constraints
684db121 268
269=head1 METHODS
270
ba05bb84 271=over
3a29a080 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
be06d3b0 285=item C<< $constraint->is_a_type_of($name or $object) >>
3a29a080 286
287=item C<< $constraint->coerce($value) >>
288
289=item C<< $constraint->check($value) >>
290
291=item C<< $constraint->assert_valid($value) >>
684db121 292
3a29a080 293=item C<< $constraint->get_message($value) >>
684db121 294
3a29a080 295=item C<< $constraint->create_child_type(%options) >>
684db121 296
297=back
298
1820fffe 299=head1 SEE ALSO
300
301L<Moose::Meta::TypeConstraint>
302
684db121 303=cut
304