Hello there,
I'm a bit puzzled by something I found when hacking on Coat and I'd like
to have your point of view on this.
Let's say we have the folloiwng types:
subtype 'Date'
=> as 'Str'
=> where { /^\d\d\d\d-\d\d-\d\d$/ };
subtype 'DateTime'
=> as 'Str'
=> where { /^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d$/ };
coerce 'DateTime'
=> from 'Date'
=> via { "$_ 00:00:00" };
And a class that has two attributes:
{
package Foo;
use Moose;
has 'date' => (
is => 'rw',
isa => 'Date',
);
has 'date_time' => (
is => 'rw',
isa => 'DateTime',
coerce => 1,
);
}
This works pretty well: as expected, I can coerce date_time from a
Date-valid value :
Perl> my $f = Foo->new
Foo=HASH(0x85567d0)
Perl> $f->date_time('2008-02-11')
2008-02-11 00:00:00
Now - and that's where the issue gets in the scene - if I add another
coercion for the DateTime subtype, but from another source, it won't work :
coerce 'DateTime'
=> from 'Int'
=> via { time_to_datetime($_) };
Perl> use Foo
[!] The type coercion for 'DateTime' has already been registered at
/usr/share/perl5/Moose/Util/TypeConstraints.pm line 289
Moose::Util::TypeConstraints::_install_type_coercions('DateTime',
'ARRAY(0x862add0)') called at
/usr/share/perl5/Moose/Util/TypeConstraints.pm line 218
Moose::Util::TypeConstraints::coerce('DateTime', 'Date',
'CODE(0x862a8b4)') called at Foo.pm line 37
require Foo.pm called at (eval 2) line 3
So it looks like, with Moose, when you define a coercion, a subtype is
defined under the table, hence it's not possible to define more than one
coercion for a given type.
Am I doing something wrong here? Is it supposed to be possible? If not, why?
For the record this works pretty well with Coat/Coat::Types and I think
it should with Moose.
Thanks.
Alexis.