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};
20 if($check = delete $args{optimized}) {
21 $args{hand_optimized_type_constraint} = $check;
22 $args{compiled_type_constraint} = $check;
24 elsif(my $param = $args{type_parameter}) {
25 my $generator = $args{constraint_generator}
26 || $class->throw_error("The $args{name} constraint cannot be used,"
27 . " because $param doesn't subtype from a parameterizable type");
28 # it must be 'constraint'
29 $check = $args{constraint} = $generator->($param);
32 $check = $args{constraint};
35 if(defined($check) && ref($check) ne 'CODE'){
37 "Constraint for $args{name} is not a CODE reference");
40 my $self = bless \%args, $class;
41 $self->compile_type_constraint()
42 if !$args{hand_optimized_type_constraint};
44 if($args{type_constraints}) {
45 $self->_compile_union_type_coercion();
50 sub create_child_type {
52 return ref($self)->new(@_, parent => $self);
63 sub __is_parameterized;
65 sub _compiled_type_constraint;
66 sub _compiled_type_coercion;
68 sub compile_type_constraint;
71 sub _add_type_coercions { # ($self, @pairs)
74 if(exists $self->{type_constraints}){ # union type
76 "Cannot add additional type coercions to Union types '$self'");
79 my $coercions = ($self->{coercion_map} ||= []);
80 my %has = map{ $_->[0] => undef } @{$coercions};
82 for(my $i = 0; $i < @_; $i++){
84 my $action = $_[++$i];
86 if(exists $has{$from}){
87 $self->throw_error("A coercion action already exists for '$from'");
90 my $type = Mouse::Util::TypeConstraints::find_or_parse_type_constraint($from)
91 or $self->throw_error(
92 "Could not find the type constraint ($from) to coerce from");
94 push @{$coercions}, [ $type => $action ];
97 $self->_compile_type_coercion();
101 sub _compile_type_coercion {
104 my @coercions = @{$self->{coercion_map}};
106 $self->{_compiled_type_coercion} = sub {
108 foreach my $pair (@coercions) {
109 #my ($constraint, $converter) = @$pair;
110 if ($pair->[0]->check($thing)) {
112 return $pair->[1]->($thing);
120 sub _compile_union_type_coercion {
124 foreach my $type(@{$self->{type_constraints}}){
125 if($type->has_coercion){
126 push @coercions, $type;
130 $self->{_compiled_type_coercion} = sub {
132 foreach my $type(@coercions){
133 my $value = $type->coerce($thing);
134 return $value if $self->check($value);
144 return $_[0] if $self->check(@_);
146 my $coercion = $self->{_compiled_type_coercion}
147 or $self->throw_error("Cannot coerce without a type coercion");
148 return $coercion->(@_);
152 my ($self, $value) = @_;
153 if ( my $msg = $self->message ) {
155 return $msg->($value);
158 if(not defined $value) {
161 elsif( ref($value) && defined(&overload::StrVal) ) {
162 $value = overload::StrVal($value);
164 return "Validation failed for '$self' with value $value";
169 my($self, $other) = @_;
171 # ->is_a_type_of('__ANON__') is always false
172 return 0 if !ref($other) && $other eq '__ANON__';
174 (my $other_name = $other) =~ s/\s+//g;
176 return 1 if $self->name eq $other_name;
178 if(exists $self->{type_constraints}){ # union
179 foreach my $type(@{$self->{type_constraints}}) {
180 return 1 if $type->name eq $other_name;
184 for(my $p = $self->parent; defined $p; $p = $p->parent) {
185 return 1 if $p->name eq $other_name;
191 # See also Moose::Meta::TypeConstraint::Parameterizable
193 my($self, $param, $name) = @_;
196 require Mouse::Util::TypeConstraints;
197 $param = Mouse::Util::TypeConstraints::find_or_create_isa_type_constraint($param);
200 $name ||= sprintf '%s[%s]', $self->name, $param->name;
201 return Mouse::Meta::TypeConstraint->new(
204 type_parameter => $param,
209 my ($self, $value) = @_;
211 if(!$self->check($value)){
212 $self->throw_error($self->get_message($value));
217 sub _as_string { $_[0]->name } # overload ""
218 sub _identity; # overload 0+
220 sub _unite { # overload infix:<|>
222 require Mouse::Util::TypeConstraints;
223 return Mouse::Util::TypeConstraints::find_or_parse_type_constraint(
233 Mouse::Meta::TypeConstraint - The Mouse Type Constraint metaclass
237 This document describes Mouse version 0.76
241 This class represents a type constraint, including built-in
242 type constraints, union type constraints, parameterizable/
243 parameterized type constraints, as well as custom type
250 =item C<< Mouse::Meta::TypeConstraint->new(%options) >>
252 =item C<< $constraint->name >>
254 =item C<< $constraint->parent >>
256 =item C<< $constraint->constraint >>
258 =item C<< $constraint->has_coercion >>
260 =item C<< $constraint->message >>
262 =item C<< $constraint->is_a_subtype_of($name or $object) >>
264 =item C<< $constraint->coerce($value) >>
266 =item C<< $constraint->check($value) >>
268 =item C<< $constraint->assert_valid($value) >>
270 =item C<< $constraint->get_message($value) >>
272 =item C<< $constraint->create_child_type(%options) >>
278 L<Moose::Meta::TypeConstraint>