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",
28 $type_parameter = undef;
35 if($check = delete $args{optimized}) { # likely to be builtins
36 $args{hand_optimized_type_constraint} = $check;
37 $args{compiled_type_constraint} = $check;
39 elsif(defined $type_parameter) { # parameterizing
40 my $generator = $args{constraint_generator}
41 || $class->throw_error(
42 "The $args{name} constraint cannot be used,"
43 . " because $type_parameter doesn't subtype"
44 . " from a parameterizable type");
46 my $parameterized_check = $generator->($type_parameter);
47 if(defined(my $my_check = $args{constraint})) {
49 return $parameterized_check->($_) && $my_check->($_);
53 $check = $parameterized_check;
55 $args{constraint} = $check;
58 $check = $args{constraint};
61 if(defined($check) && ref($check) ne 'CODE'){
63 "Constraint for $args{name} is not a CODE reference");
66 my $self = bless \%args, $class;
67 $self->compile_type_constraint()
68 if !$args{hand_optimized_type_constraint};
70 if($args{type_constraints}) { # union types
71 foreach my $type(@{$self->{type_constraints}}){
72 if($type->has_coercion){
73 # set undef for has_coercion()
74 $self->{_compiled_type_coercion} = undef;
83 sub create_child_type {
85 return ref($self)->new(@_, parent => $self);
96 sub __is_parameterized;
98 sub _compiled_type_constraint;
99 sub _compiled_type_coercion;
101 sub compile_type_constraint;
104 sub _add_type_coercions { # ($self, @pairs)
107 if(exists $self->{type_constraints}){ # union type
109 "Cannot add additional type coercions to Union types '$self'");
112 my $coercion_map = ($self->{coercion_map} ||= []);
113 my %has = map{ $_->[0]->name => undef } @{$coercion_map};
115 for(my $i = 0; $i < @_; $i++){
117 my $action = $_[++$i];
119 if(exists $has{$from}){
120 $self->throw_error("A coercion action already exists for '$from'");
123 my $type = Mouse::Util::TypeConstraints::find_or_parse_type_constraint($from)
124 or $self->throw_error(
125 "Could not find the type constraint ($from) to coerce from");
127 push @{$coercion_map}, [ $type => $action ];
130 $self->{_compiled_type_coercion} = undef;
134 sub _compiled_type_coercion {
137 my $coercion = $self->{_compiled_type_coercion};
138 return $coercion if defined $coercion;
140 if(!$self->{type_constraints}) {
142 foreach my $pair(@{$self->{coercion_map}}) {
144 [ $pair->[0]->_compiled_type_constraint, $pair->[1] ];
149 foreach my $pair (@coercions) {
150 #my ($constraint, $converter) = @$pair;
151 if ($pair->[0]->($thing)) {
153 return $pair->[1]->($thing);
159 else { # for union type
161 foreach my $type(@{$self->{type_constraints}}){
162 if($type->has_coercion){
163 push @coercions, $type;
169 foreach my $type(@coercions){
170 my $value = $type->coerce($thing);
171 return $value if $self->check($value);
178 return( $self->{_compiled_type_coercion} = $coercion );
183 return $_[0] if $self->check(@_);
185 my $coercion = $self->_compiled_type_coercion
186 or $self->throw_error("Cannot coerce without a type coercion");
187 return $coercion->(@_);
191 my ($self, $value) = @_;
192 if ( my $msg = $self->message ) {
194 return $msg->($value);
197 if(not defined $value) {
200 elsif( ref($value) && defined(&overload::StrVal) ) {
201 $value = overload::StrVal($value);
203 return "Validation failed for '$self' with value $value";
208 my($self, $other) = @_;
210 # ->is_a_type_of('__ANON__') is always false
211 return 0 if !ref($other) && $other eq '__ANON__';
213 (my $other_name = $other) =~ s/\s+//g;
215 return 1 if $self->name eq $other_name;
217 if(exists $self->{type_constraints}){ # union
218 foreach my $type(@{$self->{type_constraints}}) {
219 return 1 if $type->name eq $other_name;
223 for(my $p = $self->parent; defined $p; $p = $p->parent) {
224 return 1 if $p->name eq $other_name;
230 # See also Moose::Meta::TypeConstraint::Parameterizable
232 my($self, $param, $name) = @_;
235 require Mouse::Util::TypeConstraints;
236 $param = Mouse::Util::TypeConstraints::find_or_create_isa_type_constraint($param);
239 $name ||= sprintf '%s[%s]', $self->name, $param->name;
240 return Mouse::Meta::TypeConstraint->new(
243 type_parameter => $param,
248 my ($self, $value) = @_;
250 if(!$self->check($value)){
251 $self->throw_error($self->get_message($value));
258 sub _as_string { $_[0]->name } # overload ""
259 sub _identity; # overload 0+
261 sub _unite { # overload infix:<|>
263 require Mouse::Util::TypeConstraints;
264 return Mouse::Util::TypeConstraints::_find_or_create_union_type(
266 Mouse::Util::TypeConstraints::find_or_create_isa_type_constraint($rhs),
275 Mouse::Meta::TypeConstraint - The Mouse Type Constraint metaclass
279 This document describes Mouse version 0.94
283 This class represents a type constraint, including built-in
284 type constraints, union type constraints, parameterizable/
285 parameterized type constraints, as well as custom type
292 =item C<< Mouse::Meta::TypeConstraint->new(%options) >>
294 =item C<< $constraint->name >>
296 =item C<< $constraint->parent >>
298 =item C<< $constraint->constraint >>
300 =item C<< $constraint->has_coercion >>
302 =item C<< $constraint->message >>
304 =item C<< $constraint->is_a_type_of($name or $object) >>
306 =item C<< $constraint->coerce($value) >>
308 =item C<< $constraint->check($value) >>
310 =item C<< $constraint->assert_valid($value) >>
312 =item C<< $constraint->get_message($value) >>
314 =item C<< $constraint->create_child_type(%options) >>
320 L<Moose::Meta::TypeConstraint>