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
perl perl-mouse


source to share


No one has answered this question yet

See similar questions:

1
Constraint of type "XYZ" has already been created

or similar:

473
Why is this program valid? I was trying to create a syntax error
4
Is it possible to automatically enforce parameters passed to delegated methods (from an array property) with Moose / MooseX :: Declare for Perl?
3
Can MooseX modules be used with a mouse class?
3
Bread :: Board - Constrained input parameters of type ArrayRef?
3
How does the Moose attribute "make" the mouse role?
2
Perl: fix Moose attributes and types
2
Parameterizing a type with a different type using Type :: Tiny
1
Initializing Perl / Moose object with coercion in ArrayRef
1
Constraint of type "XYZ" has already been created
0
Questions about the meta object of the same name



All Articles
Loading...
X
Show
Funny
Dev
Pics