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 ($first, @rest) = @{ $self->type_constraints };
136 for my $parent ( $first->_collect_all_parents ) {
137 return $parent if all { $_->is_a_type_of($parent) } @rest;
144 my ($self, $value) = @_;
146 foreach my $type (@{$self->type_constraints}) {
147 my $err = $type->validate($value);
148 return unless defined $err;
149 $message .= ($message ? ' and ' : '') . $err
152 return ($message . ' in (' . $self->name . ')') ;
156 my ($self, $value) = @_;
158 return first { $_->check($value) } @{ $self->type_constraints };
162 my ($self, $type_name) = @_;
164 return all { $_->is_a_type_of($type_name) } @{ $self->type_constraints };
168 my ($self, $type_name) = @_;
170 return all { $_->is_subtype_of($type_name) } @{ $self->type_constraints };
173 sub create_child_type {
174 my ( $self, %opts ) = @_;
177 = Moose::Meta::TypeConstraint->new( %opts, parent => $self );
179 # if we have a type constraint union, and no
180 # type check, this means we are just aliasing
181 # the union constraint, which means we need to
182 # handle this differently.
184 if ( not( defined $opts{constraint} )
185 && $self->has_coercion ) {
186 $constraint->coercion(
187 Moose::Meta::TypeCoercion::Union->new(
188 type_constraint => $self,
198 # ABSTRACT: A union of Moose type constraints
206 This metaclass represents a union of type constraints. A union takes
207 multiple type constraints, and is true if any one of its member
212 C<Moose::Meta::TypeConstraint::Union> is a subclass of
213 L<Moose::Meta::TypeConstraint>.
217 =item B<< Moose::Meta::TypeConstraint::Union->new(%options) >>
219 This creates a new class type constraint based on the given
222 It takes the same options as its parent. It also requires an
223 additional option, C<type_constraints>. This is an array reference
224 containing the L<Moose::Meta::TypeConstraint> objects that are the
225 members of the union type. The C<name> option defaults to the names
226 all of these member types sorted and then joined by a pipe (|).
228 The constructor sets the implementation of the constraint so that is
229 simply calls C<check> on the newly created object.
231 Finally, the constructor also makes sure that the object's C<coercion>
232 attribute is a L<Moose::Meta::TypeCoercion::Union> object.
234 =item B<< $constraint->type_constraints >>
236 This returns the array reference of C<type_constraints> provided to
239 =item B<< $constraint->parent >>
241 This returns the nearest common ancestor of all the components of the union.
243 =item B<< $constraint->check($value) >>
245 =item B<< $constraint->validate($value) >>
247 These two methods simply call the relevant method on each of the
248 member type constraints in the union. If any type accepts the value,
251 With C<validate> the error message returned includes all of the error
252 messages returned by the member type constraints.
254 =item B<< $constraint->equals($type_name_or_object) >>
256 A type is considered equal if it is also a union type, and the two
257 unions have the same member types.
259 =item B<< $constraint->find_type_for($value) >>
261 This returns the first member type constraint for which C<check($value)> is
262 true, allowing you to determine which of the Union's member type constraints
263 a given value matches.
265 =item B<< $constraint->is_a_type_of($type_name_or_object) >>
267 This returns true if all of the member type constraints return true
268 for the C<is_a_type_of> method.
270 =item B<< $constraint->is_subtype_of >>
272 This returns true if all of the member type constraints return true
273 for the C<is_a_subtype_of> method.
275 =item B<< $constraint->create_child_type(%options) >>
277 This returns a new L<Moose::Meta::TypeConstraint> object with the type
284 See L<Moose/BUGS> for details on reporting bugs.