Skip Menu |

This queue is for tickets about the Type-Tiny CPAN distribution.

Report information
The Basics
Id: 86172
Status: resolved
Priority: 0/
Queue: Type-Tiny

People
Owner: perl [...] toby.ink
Requestors: Support [...] RoxSoft.co.uk
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: (no value)
Fixed in: (no value)



Subject: Missing coercion with Moose and Type::Tiny
If you have a Moose class that inherits from a Moo class that has an attribute with a Type::Tiny type constraint which defines a coercion, then the coercion disappears and you get the following error You cannot coerce an attribute (path) unless its type (Directory) has a coercion at /usr/local/share/perl/5.10.1/Moo/HandleMoose.pm line 146. Test and patch attached Regards
Subject: MyTypes.pm
package MyTypes; use strict; use warnings; use namespace::clean -except => 'meta'; use Type::Library -base, -declare => qw( Path Directory ); use Type::Utils; BEGIN { extends q(Types::Standard) }; { package IO::Class; sub new { shift; bless { path => $_[ 0 ] }, 'IO::Class' } sub is_dir { -d $_[ 0 ]->path } sub path { $_[ 0 ]->{path} } } declare Path, as Object, where { $_->isa( q(IO::Class) ) }, message { 'Wrong class' }; declare Directory, as Path, where { $_->is_dir }, message { 'Not a directory' }; coerce Directory, from Str, via { IO::Class->new( $_ ) }; coerce Path, from Str, via { IO::Class->new( $_ ) }; 1; __END__
Subject: moo-handlemoose.patch
--- HandleMoose.pm 2013-06-15 21:33:19.000000000 +0100 +++ HandleMoose.pm 2013-06-15 21:34:29.000000000 +0100 @@ -114,7 +114,7 @@ my $type = $mapped->(); Scalar::Util::blessed($type) && $type->isa("Moose::Meta::TypeConstraint") or die "error inflating attribute '$name' for package '$_[0]': \$TYPE_MAP{$isa} did not return a valid type constraint'"; - $coerce ? $type->create_child_type(name => $type->name) : $type; + $coerce ? $type->create_child_type(name => $type->name, coercion => 1) : $type; } else { Moose::Meta::TypeConstraint->new( constraint => sub { eval { &$isa; 1 } }
Subject: moose-type_tiny-coercion.t
use strictures 1; use Test::More; use Test::Exception; use lib 'xt/lib'; { package TC1; use Moo; use MyTypes qw( Directory ); has 'path' => is => 'ro', isa => Directory, coerce => Directory->coercion; } { package TC2; use Moose; extends 'TC1'; } my $tc; eval { $tc = TC2->new( path => 't' ) }; ok defined $tc, 'Failed to construct coercion test case'; defined $tc and is $tc->path->path, 't', 'Moose + Inheritance + Type::Tiny + Coercion'; done_testing;
An interesting one. I think this is probably better solved from within Type::Coercion. Supporting $type->coercion->_compiled_type_coercion($c) as a method of adding coercions to a type constraint, where $c is a coderef or a blessed Type::Coercion object, ought to do the trick.
I asked Toby about this and we agreed it's more of an issue for Type::Tiny to solve, not Moo itself. I've moved it to Type::Tiny's queue.
This patch seems to solve it... https://github.com/tobyink/p5-type-tiny/commit/271aa3ea0c0fe71696db15086a876d7843669a04 I'll try to get another developer release out later today.
Fixed in 0.008.