From: gfx Date: Fri, 30 Oct 2009 03:00:41 +0000 (+0900) Subject: Move parametarization code to Meta::TypeConstraint X-Git-Tag: 0.40_03~2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMouse.git;a=commitdiff_plain;h=b4d791ba8c0469589992854be1b6c40c80ec4f0a Move parametarization code to Meta::TypeConstraint --- diff --git a/lib/Mouse/Meta/TypeConstraint.pm b/lib/Mouse/Meta/TypeConstraint.pm index 30edadd..a786ced 100644 --- a/lib/Mouse/Meta/TypeConstraint.pm +++ b/lib/Mouse/Meta/TypeConstraint.pm @@ -234,6 +234,28 @@ sub is_a_type_of{ return 0; } +# See also Moose::Meta::TypeConstraint::Parameterizable +sub parameterize{ + my($self, $param, $name) = @_; + + if(!ref $param){ + require Mouse::Util::TypeConstraints; + $param = Mouse::Util::TypeConstraints::find_or_create_isa_type_constraint($param); + } + + $name ||= sprintf '%s[%s]', $self->name, $param->name; + + my $generator = $self->{constraint_generator} + || Carp::confess("The $name constraint cannot be used, because $param doesn't subtype from a parameterizable type"); + + return Mouse::Meta::TypeConstraint->new( + name => $name, + parent => $self, + constraint => $generator->($param), + + type => 'Parameterized', + ); +} 1; __END__ diff --git a/lib/Mouse/Util/TypeConstraints.pm b/lib/Mouse/Util/TypeConstraints.pm index 7ae23f3..098e0ef 100644 --- a/lib/Mouse/Util/TypeConstraints.pm +++ b/lib/Mouse/Util/TypeConstraints.pm @@ -263,35 +263,20 @@ sub _find_or_create_parameterized_type{ my $name = sprintf '%s[%s]', $base->name, $param->name; - $TYPE{$name} ||= do{ - my $generator = $base->{constraint_generator}; - - if(!$generator){ - confess("The $name constraint cannot be used, because $param doesn't subtype from a parameterizable type"); - } - - Mouse::Meta::TypeConstraint->new( - name => $name, - parent => $base, - constraint => $generator->($param), - - type => 'Parameterized', - ); - } + $TYPE{$name} ||= $base->parameterize($param, $name); } + sub _find_or_create_union_type{ my @types = sort map{ $_->{type_constraints} ? @{$_->{type_constraints}} : $_ } @_; my $name = join '|', @types; - $TYPE{$name} ||= do{ - return Mouse::Meta::TypeConstraint->new( - name => $name, - type_constraints => \@types, + $TYPE{$name} ||= Mouse::Meta::TypeConstraint->new( + name => $name, + type_constraints => \@types, - type => 'Union', - ); - }; + type => 'Union', + ); } # The type parser