Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coerce documentation is missing important fact that it must be requested in attribute definition. #116

Open
bbkr opened this issue Apr 20, 2022 · 0 comments

Comments

@bbkr
Copy link

bbkr commented Apr 20, 2022

By reading https://metacpan.org/pod/Mouse::Util::TypeConstraints documentation i felt into the trap and spent good amount of time trying to figure out why following code is not working:

package A;

use Mouse;
use Mouse::Util::TypeConstraints;

subtype 'MultiInt' => as 'ArrayRef[Int]';
coerce 'MultiInt' => from 'Int' => via { [ $_ ] };

has 'a' => ('is' => 'ro', 'isa' => 'MultiInt');

and A->new( 'a' => 1 ) was throwing me this error:

Attribute (a) does not pass the type constraint because: Validation failed for 'MultiInt' with value 1 at 
...

Fix:

This documentation page should mention, that coerce should be explicitly requested in attribute definition: has 'a' => ('is' => 'ro', 'isa' => 'MultiInt', 'coerce' => 1);. Good example is Moose documentation that says is explicitly: Moose will never try to coerce a value unless you explicitly ask for it. This is done by setting the coerce attribute option to a true value. While in Mouse this important piece of information is not mentioned in Mouse::Util::TypeConstraints nor Moose::Manual::Attributes doc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant