2 package Moose::Meta::TypeCoercion::Union;
8 use Scalar::Util 'blessed';
10 our $AUTHORITY = 'cpan:STEVAN';
12 use base 'Moose::Meta::TypeCoercion';
14 sub compile_type_coercion {
16 my $type_constraint = $self->type_constraint;
18 (blessed $type_constraint && $type_constraint->isa('Moose::Meta::TypeConstraint::Union'))
19 || Moose->throw_error("You can only create a Moose::Meta::TypeCoercion::Union for a " .
20 "Moose::Meta::TypeConstraint::Union, not a $type_constraint");
22 $self->_compiled_type_coercion(
26 foreach my $type ( grep { $_->has_coercion }
27 @{ $type_constraint->type_constraints } ) {
28 my $temp = $type->coerce($value);
29 return $temp if $type_constraint->check($temp);
37 sub has_coercion_for_type { 0 }
39 sub add_type_coercions {
41 Moose->throw_error("Cannot add additional type coercions to Union types");
46 # ABSTRACT: The Moose Type Coercion metaclass for Unions
54 This is a subclass of L<Moose::Meta::TypeCoercion> that is used for
55 L<Moose::Meta::TypeConstraint::Union> objects.
60 =item B<< $coercion->has_coercion_for_type >>
62 This method always returns false.
64 =item B<< $coercion->add_type_coercions >>
66 This method always throws an error. You cannot add coercions to a
69 =item B<< $coercion->coerce($value) >>
71 This method will coerce by trying the coercions for each type in the
78 See L<Moose/BUGS> for details on reporting bugs.