Fix subtype()
[gitmo/Mouse.git] / lib / Mouse / Util / TypeConstraints.pm
index fb63f0c..db543f4 100644 (file)
@@ -72,9 +72,8 @@ BEGIN {
 
     while (my ($name, $code) = each %builtins) {
         $TYPE{$name} = Mouse::Meta::TypeConstraint->new(
-            name                      => $name,
-            _compiled_type_constraint => $code,
-            package_defined_in        => __PACKAGE__,
+            name      => $name,
+            optimized => $code,
         );
     }
 
@@ -125,17 +124,15 @@ sub _create_type{
               . "$existing->{package_defined_in} and cannot be created again in $package_defined_in");
     }
 
-    $args{constraint}                = delete($args{where})
-        if exists $args{where};
-    $args{_compiled_type_constraint} = delete $args{optimized_as}
-        if exists $args{optimized_as};
+    $args{constraint} = delete($args{where})       if exists $args{where};
+    $args{optimized}  = delete $args{optimized_as} if exists $args{optimized_as};
 
     my $constraint;
     if($mode eq 'subtype'){
-        my $parent = exists($args{as}) ? delete($args{as}) : delete($args{name});
+        my $parent = delete($args{as})
+            or confess('A subtype cannot consist solely of a name, it must have a parent');
 
-        $parent     = find_or_create_isa_type_constraint($parent);
-        $constraint = $parent->create_child_type(%args);
+        $constraint = find_or_create_isa_type_constraint($parent)->create_child_type(%args);
     }
     else{
         $constraint = Mouse::Meta::TypeConstraint->new(%args);
@@ -281,7 +278,7 @@ sub _find_or_create_regular_type{
 
     my $check;
     my $type;
-    if($meta && $meta->isa('Mouse::Meta::Role')){
+    if($meta->isa('Mouse::Meta::Role')){
         $check = sub{
             return blessed($_[0]) && $_[0]->does($spec);
         };
@@ -297,16 +294,16 @@ sub _find_or_create_regular_type{
     warn "#CREATE a $type type for $spec\n" if _DEBUG;
 
     return $TYPE{$spec} = Mouse::Meta::TypeConstraint->new(
-        name                      => $spec,
-        _compiled_type_constraint => $check,
+        name      => $spec,
+        optimized => $check,
 
-        type                      => $type,
+        type      => $type,
     );
 }
 
 $TYPE{ArrayRef}{constraint_generator} = sub {
     my($type_parameter) = @_;
-    my $check = $type_parameter->{_compiled_type_constraint};
+    my $check = $type_parameter->_compiled_type_constraint;
 
     return sub{
         foreach my $value (@{$_}) {
@@ -317,7 +314,7 @@ $TYPE{ArrayRef}{constraint_generator} = sub {
 };
 $TYPE{HashRef}{constraint_generator} = sub {
     my($type_parameter) = @_;
-    my $check = $type_parameter->{_compiled_type_constraint};
+    my $check = $type_parameter->_compiled_type_constraint;
 
     return sub{
         foreach my $value(values %{$_}){
@@ -330,7 +327,7 @@ $TYPE{HashRef}{constraint_generator} = sub {
 # 'Maybe' type accepts 'Any', so it requires parameters
 $TYPE{Maybe}{constraint_generator} = sub {
     my($type_parameter) = @_;
-    my $check = $type_parameter->{_compiled_type_constraint};
+    my $check = $type_parameter->_compiled_type_constraint;
 
     return sub{
         return !defined($_) || $check->($_);
@@ -361,27 +358,18 @@ sub _find_or_create_parameterized_type{
     }
 }
 sub _find_or_create_union_type{
-    my @types              = map{ $_->{type_constraints} ? @{$_->{type_constraints}} : $_ } @_;
+    my @types = map{ $_->{type_constraints} ? @{$_->{type_constraints}} : $_ } @_;
 
     my $name = join '|', map{ $_->name } @types;
 
     $TYPE{$name} ||= do{
         warn "# CREATE a Union type for ", Mouse::Util::english_list(@types),"\n" if _DEBUG;
 
-        my @checks = map{ $_->{_compiled_type_constraint} } @types;
-        my $check = sub{
-            foreach my $c(@checks){
-                return 1 if $c->($_[0]);
-            }
-            return 0;
-        };
-
         return Mouse::Meta::TypeConstraint->new(
-            name                      => $name,
-            _compiled_type_constraint => $check,
-            type_constraints          => \@types,
+            name              => $name,
+            type_constraints  => \@types,
 
-            type                      => 'Union',
+            type              => 'Union',
         );
     };
 }
@@ -400,7 +388,7 @@ sub _parse_type{
         my $char = substr($spec, $i, 1);
 
         if($char eq '['){
-            my $base = _find_or_create_regular_type( substr($spec, $start, $i - $start))
+            my $base = _find_or_create_regular_type( substr($spec, $start, $i - $start) )
                 or return;
 
             ($i, $subtype) = _parse_type($spec, $i+1)
@@ -414,8 +402,12 @@ sub _parse_type{
             last;
         }
         elsif($char eq '|'){
-            my $type = _find_or_create_regular_type( substr($spec, $start, $i - $start))
-                or return;
+            my $type = _find_or_create_regular_type( substr($spec, $start, $i - $start) );
+
+            if(!defined $type){
+                # XXX: Mouse creates a new class type, but Moose does not.
+                $type = class_type( substr($spec, $start, $i - $start) );
+            }
 
             push @list, $type;
 
@@ -642,9 +634,13 @@ related C<eq_deeply> function.
 
 =head1 METHODS
 
-=head2 optimized_constraints -> HashRef[CODE]
+=head2 C<< list_all_builtin_type_constraints -> (Names) >>
+
+Returns the names of builtin type constraints.
+
+=head2 C<< list_all_type_constraints -> (Names) >>
 
-Returns the simple type constraints that Mouse understands.
+Returns the names of all the type constraints.
 
 =head1 FUNCTIONS