Mouse type constraint implicitly created

I am trying to coerce from type ArrayRef[HashRef]

to ArrayRef[MyModule::Object]

, but for some reason I am getting an error. Below is a class of type constraints:

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 a ref array 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 class MyModule::Object

:

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 to arrayref hashes:

my $obj = MyModule->new(objects => [{x => 1}, {x => 2}, {x => 3}]);

      

I am getting 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 this has something to do with me, possibly use MyModule::Types

in several places. But I need to use it in multiple places so that the modules can be used on their own. Also, for some reason other types (I have other types defined in the TypeConstraints file) don't seem to give me this error. Shouldn't Mouse :: Util :: TypeConstraints handle the included modules that can be used together so that modules can be used individually?

+3


source to share





All Articles