@@ -12,6 +12,7 @@ use MooseX::Types::Util qw( filter_tags );
1212use MooseX::Types::UndefinedType;
1313use MooseX::Types::CheckedUtilExports ();
1414use Carp::Clan qw( ^MooseX::Types ) ;
15+ use Sub::Defer qw( defer_sub ) ;
1516use Sub::Name;
1617use Scalar::Util qw( reftype ) ;
1718use Sub::Exporter::ForMethods 0.100052 ' method_installer' ; # for 'rebless'
@@ -486,17 +487,20 @@ This generates a coercion handler function, e.g. C<to_Int($value)>.
486487
487488sub coercion_export_generator {
488489 my ($class , $type , $full , $undef_msg ) = @_ ;
489- return sub {
490+ return defer_sub undef , sub {
490491 my ($value ) = @_ ;
491492
492493 # we need a type object
493494 my $tobj = find_type_constraint($full ) or croak $undef_msg ;
494- my $return = $tobj -> coerce($value );
495495
496- # non-successful coercion returns false
497- return unless $tobj -> check( $return );
496+ return sub {
497+ my $ return = $tobj -> coerce( $_ [0] );
498498
499- return $return ;
499+ # non-successful coercion returns false
500+ return unless $tobj -> check($return );
501+
502+ return $return ;
503+ };
500504 }
501505}
502506
@@ -508,13 +512,16 @@ Generates a constraint check closure, e.g. C<is_Int($value)>.
508512
509513sub check_export_generator {
510514 my ($class , $type , $full , $undef_msg ) = @_ ;
511- return sub {
515+
516+ return defer_sub undef , sub {
512517 my ($value ) = @_ ;
513518
514519 # we need a type object
515520 my $tobj = find_type_constraint($full ) or croak $undef_msg ;
516521
517- return $tobj -> check($value );
522+ # This method will actually compile an inlined sub if possible. If
523+ # not, it will return something like sub { $tobj->check($_[0]) }
524+ return $tobj -> _compiled_type_constraint;
518525 }
519526}
520527
0 commit comments