Simplify compiled type coercion for union
Dave Rolsky [Fri, 20 Aug 2010 12:22:16 +0000 (07:22 -0500)]
lib/Moose/Meta/TypeCoercion/Union.pm

index e76585d..cd00c7c 100644 (file)
@@ -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 }