2 package Moose::Meta::TypeConstraint::Union;
8 use Moose::Meta::TypeCoercion::Union;
10 use List::MoreUtils qw(all);
11 use List::Util qw(first);
13 use base 'Moose::Meta::TypeConstraint';
15 __PACKAGE__->meta->add_attribute('type_constraints' => (
16 accessor => 'type_constraints',
17 default => sub { [] },
18 Class::MOP::_definition_context(),
22 my ($class, %options) = @_;
24 my $name = join '|' => sort { $a cmp $b }
25 map { $_->name } @{ $options{type_constraints} };
27 my $self = $class->SUPER::new(
32 $self->_set_constraint(sub { $self->check($_[0]) });
37 # XXX - this is a rather gross implementation of laziness for the benefit of
38 # MX::Types. If we try to call ->has_coercion on the objects during object
39 # construction, this does not work when defining a recursive constraint with
44 return $self->{coercion} if exists $self->{coercion};
46 # Using any instead of grep here causes a weird error with some corner
47 # cases when MX::Types is in use. See RT #61001.
48 if ( grep { $_->has_coercion } @{ $self->type_constraints } ) {
49 return $self->{coercion} = Moose::Meta::TypeCoercion::Union->new(
50 type_constraint => $self );
53 return $self->{coercion} = undef;
58 return defined $_[0]->coercion;
61 sub _actually_compile_type_constraint {
64 my @constraints = @{ $self->type_constraints };
68 foreach my $type (@constraints) {
69 return 1 if $type->check($value);
78 # This was originally done with all() from List::MoreUtils, but that
79 # caused some sort of bizarro parsing failure under 5.10.
80 for my $tc ( @{ $self->type_constraints } ) {
81 return 0 unless $tc->can_be_inlined;
93 join ' || ', map { '(' . $_->_inline_check($val) . ')' }
94 @{ $self->type_constraints }
99 sub inline_environment {
102 return { map { %{ $_->inline_environment } }
103 @{ $self->type_constraints } };
107 my ( $self, $type_or_name ) = @_;
109 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
111 return unless $other->isa(__PACKAGE__);
113 my @self_constraints = @{ $self->type_constraints };
114 my @other_constraints = @{ $other->type_constraints };
116 return unless @self_constraints == @other_constraints;
118 # FIXME presort type constraints for efficiency?
119 constraint: foreach my $constraint ( @self_constraints ) {
120 for ( my $i = 0; $i < @other_constraints; $i++ ) {
121 if ( $constraint->equals($other_constraints[$i]) ) {
122 splice @other_constraints, $i, 1;
128 return @other_constraints == 0;
134 my @tcs = @{ $self->type_constraints };
137 = ( sort { $a->_ancestor_count <=> $b->_ancestor_count } @tcs )[-1];
139 for my $parent ( $deepest->_collect_all_parents ) {
140 return $parent if all { $_->is_a_type_of($parent) } @tcs;
145 my ($self, $value) = @_;
147 foreach my $type (@{$self->type_constraints}) {
148 my $err = $type->validate($value);
149 return unless defined $err;
150 $message .= ($message ? ' and ' : '') . $err
153 return ($message . ' in (' . $self->name . ')') ;
157 my ($self, $value) = @_;
159 return first { $_->check($value) } @{ $self->type_constraints };
163 my ($self, $type_name) = @_;
165 return all { $_->is_a_type_of($type_name) } @{ $self->type_constraints };
169 my ($self, $type_name) = @_;
171 return all { $_->is_subtype_of($type_name) } @{ $self->type_constraints };
174 sub create_child_type {
175 my ( $self, %opts ) = @_;
178 = Moose::Meta::TypeConstraint->new( %opts, parent => $self );
180 # if we have a type constraint union, and no
181 # type check, this means we are just aliasing
182 # the union constraint, which means we need to
183 # handle this differently.
185 if ( not( defined $opts{constraint} )
186 && $self->has_coercion ) {
187 $constraint->coercion(
188 Moose::Meta::TypeCoercion::Union->new(
189 type_constraint => $self,
199 # ABSTRACT: A union of Moose type constraints
207 This metaclass represents a union of type constraints. A union takes
208 multiple type constraints, and is true if any one of its member
213 C<Moose::Meta::TypeConstraint::Union> is a subclass of
214 L<Moose::Meta::TypeConstraint>.
218 =item B<< Moose::Meta::TypeConstraint::Union->new(%options) >>
220 This creates a new class type constraint based on the given
223 It takes the same options as its parent. It also requires an
224 additional option, C<type_constraints>. This is an array reference
225 containing the L<Moose::Meta::TypeConstraint> objects that are the
226 members of the union type. The C<name> option defaults to the names
227 all of these member types sorted and then joined by a pipe (|).
229 The constructor sets the implementation of the constraint so that is
230 simply calls C<check> on the newly created object.
232 Finally, the constructor also makes sure that the object's C<coercion>
233 attribute is a L<Moose::Meta::TypeCoercion::Union> object.
235 =item B<< $constraint->type_constraints >>
237 This returns the array reference of C<type_constraints> provided to
240 =item B<< $constraint->parent >>
242 This returns the nearest common ancestor of all the components of the union.
244 =item B<< $constraint->check($value) >>
246 =item B<< $constraint->validate($value) >>
248 These two methods simply call the relevant method on each of the
249 member type constraints in the union. If any type accepts the value,
252 With C<validate> the error message returned includes all of the error
253 messages returned by the member type constraints.
255 =item B<< $constraint->equals($type_name_or_object) >>
257 A type is considered equal if it is also a union type, and the two
258 unions have the same member types.
260 =item B<< $constraint->find_type_for($value) >>
262 This returns the first member type constraint for which C<check($value)> is
263 true, allowing you to determine which of the Union's member type constraints
264 a given value matches.
266 =item B<< $constraint->is_a_type_of($type_name_or_object) >>
268 This returns true if all of the member type constraints return true
269 for the C<is_a_type_of> method.
271 =item B<< $constraint->is_subtype_of >>
273 This returns true if all of the member type constraints return true
274 for the C<is_a_subtype_of> method.
276 =item B<< $constraint->create_child_type(%options) >>
278 This returns a new L<Moose::Meta::TypeConstraint> object with the type
285 See L<Moose/BUGS> for details on reporting bugs.