2 package Moose::Meta::TypeCoercion::Union;
8 use Scalar::Util 'blessed';
10 use base 'Moose::Meta::TypeCoercion';
12 sub compile_type_coercion {
14 my $type_constraint = $self->type_constraint;
16 (blessed $type_constraint && $type_constraint->isa('Moose::Meta::TypeConstraint::Union'))
17 || Moose::Util::throw("You can only create a Moose::Meta::TypeCoercion::Union for a " .
18 "Moose::Meta::TypeConstraint::Union, not a $type_constraint");
20 $self->_compiled_type_coercion(
24 foreach my $type ( grep { $_->has_coercion }
25 @{ $type_constraint->type_constraints } ) {
26 my $temp = $type->coerce($value);
27 return $temp if $type_constraint->check($temp);
35 sub has_coercion_for_type { 0 }
37 sub add_type_coercions {
38 Moose::Util::throw("Cannot add additional type coercions to Union types");
43 # ABSTRACT: The Moose Type Coercion metaclass for Unions
51 This is a subclass of L<Moose::Meta::TypeCoercion> that is used for
52 L<Moose::Meta::TypeConstraint::Union> objects.
57 =item B<< $coercion->has_coercion_for_type >>
59 This method always returns false.
61 =item B<< $coercion->add_type_coercions >>
63 This method always throws an error. You cannot add coercions to a
66 =item B<< $coercion->coerce($value) >>
68 This method will coerce by trying the coercions for each type in the
75 See L<Moose/BUGS> for details on reporting bugs.