Commits

Toby Inkster committed 486fab9

coercions from mouse

Comments (0)

Files changed (3)

 		$Moo::HandleMoose::TYPE_MAP{overload::StrVal($self)} = sub { $ALL_TYPES{$uniq} };
 	}
 	
+	if (ref($params{coercion}) eq q(CODE))
+	{
+		require Types::Standard;
+		my $code = delete($params{coercion});
+		$self->{coercion} = $self->_build_coercion;
+		$self->coercion->add_type_coercions(Types::Standard::Any(), $code);
+	}
+	elsif (ref($params{coercion}) eq q(ARRAY))
+	{
+		my $arr = delete($params{coercion});
+		$self->{coercion} = $self->_build_coercion;
+		$self->coercion->add_type_coercions(@$arr);
+	}
+	
 	$self->{type_constraints} ||= undef;
 	
 	return $self;

lib/Type/Utils.pm

 			my $types = $lib->type_storage;
 			for my $name (sort keys %$types)
 			{
-				my $tt = Types::TypeTiny::to_TypeTiny(
-					Mouse::Util::TypeConstraints::find_type_constraint($types->{$name})
+				my $mouse = Mouse::Util::TypeConstraints::find_type_constraint($types->{$name});
+				my $tt    = Types::TypeTiny::to_TypeTiny($mouse);
+				$caller->add_type(
+					$tt->create_child_type(library => $caller, name => $name, coercion => $mouse->has_coercion ? 1 : 0)
 				);
-				$caller->add_type($tt->create_child_type(library => $caller, name => $name));
 			}
 		}
 		else
 	"Mouse::Util::is_a_type_constraint accepts Type::Tiny type constraints",
 );
 
+note "Coercion...";
+
+{
+	package TmpNS1;
+	use Mouse::Util::TypeConstraints;
+	subtype 'MyInt', as 'Int';
+	coerce 'MyInt', from 'ArrayRef', via { scalar(@$_) };
+	
+	my $type = Types::TypeTiny::to_TypeTiny(find_type_constraint('MyInt'));
+	
+	::ok($type->has_coercion, 'types converted from Mouse retain coercions');
+	::is($type->coerce([qw/a b c/]), 3, '... which work');
+}
+
 done_testing;