I am trying to coerce from the type ArrayRef[HashRef]
to ArrayRef[MyModule::Object]
, but for some reason I am getting an error. Below is my type constraint class:
package MyModule::Types;
use Mouse::Util::TypeConstraints;
subtype 'CoercedArrayRefOfMyModuleObjects' => as 'ArrayRef[MyModule::Object]';
coerce 'CoercedArrayRefOfMyModuleObjects'
=> from 'ArrayRef[HashRef]'
=> via { [map { MyModule::Object->new( %{$_} ) } @{$_}] };
no Mouse::Util::TypeConstraints;
1;
And this is the class that has an array ref of MyModule::Objects:
use strict;
package MyModule;
use Mouse;
use MyModule::Types;
has objects => (
is => 'rw',
isa => 'CoercedArrayRefOfMyModuleObjects',
coerce => 1,
);
__PACKAGE__->meta->make_immutable();
1;
And this is my MyModule::Object
class:
use strict;
package MyModule::Object;
use Mouse;
has x => (
is => 'rw',
isa => 'Int',
);
__PACKAGE__->meta->make_immutable();
1;
But whenever I try to create an object and pass it an arrayref of hashes:
my $obj = MyModule->new(objects => [{x => 1}, {x => 2}, {x => 3}]);
I get the following error:
The type constraint 'CoercedArrayRefOfMyModuleObjects' has already been created in Mouse::Util::TypeConstraints and cannot be created again in MyModule::Types ('CoercedArrayRefOfMyModuleObjects' is an implicitly created type constraint) at lib/MyModule/Types.pm line 41.
Does anyone know why this is?
EDIT: I think it has to do with me maybe having use MyModule::Types
in multiple places. But I need to use it in multiple locations so that modules can be used on their own. Also, for some reason other types (I have other types defined in my TypeConstraints file) don't seem to be giving me this error. Shouldn't Mouse::Util::TypeConstraints be able to handle being included in to modules that might be used together, so that the modules could be used individually?