From: Dave Rolsky Date: Fri, 20 Aug 2010 12:22:16 +0000 (-0500) Subject: Simplify compiled type coercion for union X-Git-Tag: 1.11~9 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=dc6bb63c2e9e8cb9ad0e7f1d7ef3dde0d75d14ed;p=gitmo%2FMoose.git Simplify compiled type coercion for union --- diff --git a/lib/Moose/Meta/TypeCoercion/Union.pm b/lib/Moose/Meta/TypeCoercion/Union.pm index e76585d..cd00c7c 100644 --- a/lib/Moose/Meta/TypeCoercion/Union.pm +++ b/lib/Moose/Meta/TypeCoercion/Union.pm @@ -21,23 +21,19 @@ sub compile_type_coercion { || Moose->throw_error("You can only a Moose::Meta::TypeCoercion::Union for a " . "Moose::Meta::TypeConstraint::Union, not a $type_constraint"); - $self->_compiled_type_coercion(sub { - my $value = shift; - # go through all the type constraints - # in the union, and check em ... - foreach my $type (@{$type_constraint->type_constraints}) { - # if they have a coercion first - if ($type->has_coercion) { - # then try to coerce them ... + $self->_compiled_type_coercion( + sub { + my $value = shift; + + foreach my $type ( grep { $_->has_coercion } + @{ $type_constraint->type_constraints } ) { my $temp = $type->coerce($value); - # and if they get something - # make sure it still fits within - # the union type ... return $temp if $type_constraint->check($temp); } + + return $value; } - return $value; - }); + ); } sub has_coercion_for_type { 0 }