Add inlining for parameterizable types - add infrastructure for inlining parameterized, but it needs tests
predicate => 'has_constraint_generator',
));
+__PACKAGE__->meta->add_attribute('inline_generator' => (
+ accessor => 'inline_generator',
+ predicate => 'has_inline_generator',
+));
+
sub generate_constraint_for {
my ($self, $type) = @_;
if ( $contained_tc->isa('Moose::Meta::TypeConstraint') ) {
my $tc_name = $self->name . '[' . $contained_tc->name . ']';
return Moose::Meta::TypeConstraint::Parameterized->new(
- name => $tc_name,
- parent => $self,
- type_parameter => $contained_tc,
+ name => $tc_name,
+ parent => $self,
+ type_parameter => $contained_tc,
+ parameterized_from => $self,
);
}
else {
predicate => 'has_type_parameter',
));
+__PACKAGE__->meta->add_attribute('parameterized_from' => (
+ accessor => 'parameterized_from',
+ predicate => 'has_parameterized_from',
+));
+
sub equals {
my ( $self, $type_or_name ) = @_;
. $self->parent->name . " doesn't subtype or coerce from a parameterizable type.");
}
+sub has_inlined_type_constraint {
+ my $self = shift;
+
+ return $self->has_parameterized_from
+ && $self->has_parameterized_from->has_inline_generator;
+}
+
+sub _inline_check {
+ my $self = shift;
+
+ return
+ unless $self->has_parameterized_from
+ && $self->has_parameterized_from->has_inline_generator;
+
+ return $self->parameterized_from->generate_inline_for( $self->type, @_ );
+}
+
sub create_child_type {
my ($self, %opts) = @_;
return Moose::Meta::TypeConstraint::Parameterizable->new(%opts, parent=>$self);
use strict;
use warnings;
+use List::MoreUtils ();
use Scalar::Util qw( blessed looks_like_number reftype );
sub type { goto &Moose::Util::TypeConstraints::type }
return sub {
return $check->( ${$_} );
};
- }
+ },
+ inlined => sub {qq{ref $_[0] eq 'SCALAR' || ref $_[0] eq 'REF'}},
+ inline_generator => sub {
+ my $type_parameter = shift;
+ my $val = shift;
+ return $type_parameter->_inline_check(
+ '${ (' . $val . ') }' );
+ },
)
);
}
1;
}
- }
+ },
+ inlined => sub {qq{ref $_[0] eq 'ARRAY'}},
+ inline_generator => sub {
+ my $type_parameter = shift;
+ my $val = shift;
+ return
+ '&List::MoreUtils::all( sub { '
+ . $type_parameter->_inline_check('$_')
+ . " }, \@{$val} )";
+ },
)
);
}
1;
}
- }
+ },
+ inlined => sub {qq{ref $_[0] eq 'HASH'}},
+ inline_generator => sub {
+ my $type_parameter = shift;
+ my $val = shift;
+ return
+ '&List::MoreUtils::all( sub { '
+ . $type_parameter->_inline_check('$_')
+ . " }, values \%{$val} )";
+ },
)
);
return 1 if not( defined($_) ) || $check->($_);
return;
}
- }
+ },
+ inlined => sub {'1'},
+ inline_generator => sub {
+ my $type_parameter = shift;
+ my $val = shift;
+ return
+ "(! defined $val) || ("
+ . $type_parameter->_inline_check($val) . ')';
+ },
)
);
}
$OBJECT,
],
},
+ Maybe => {
+ accept => [
+ $ZERO,
+ $ONE,
+ $INT,
+ $NEG_INT,
+ $NUM,
+ $NEG_NUM,
+ $EMPTY_STRING,
+ $STRING,
+ $NUM_IN_STRING,
+ $SCALAR_REF,
+ $SCALAR_REF_REF,
+ $ARRAY_REF,
+ $HASH_REF,
+ $CODE_REF,
+ $GLOB,
+ $GLOB_REF,
+ $FH,
+ $FH_OBJECT,
+ $REGEX,
+ $REGEX_OBJ,
+ $OBJECT,
+ $UNDEF,
+ ],
+ },
Value => {
accept => [
$ZERO,