Subject: | attribute coerce with deep type doesn't see subtype coercions |
Given an attribute ArrayRef[MyInt] with coerce => 1, Moose will
throw the error:
ou cannot coerce an attribute (bar) unless its type (ArrayRef[MyInt])
Even though it *will* respect coercions on the nested type.
To fix this, we add a redundant:
coerce 'ArrayRef[MyInt]', from 'Str', via {}
And it now runs without error AND respects the MyInt coercion (and
naturally ignores the ArrayRef[MyInt] coercion).
See attached file, which simply fails.
Subject: | arrayref_coerce.pl |
#!perl
package Foo;
use Moose;
use Moose::Util::TypeConstraints;
subtype MyInt => as 'Int';
coerce MyInt => from 'Str', via {
!$_ ? 0 : '';
};
has 'bar' => (
traits => ['Array'],
isa => 'ArrayRef[MyInt]',
coerce => 1,
handles => {
add => 'push',
},
);
1;
package main;
Foo->new->add(0);
Foo->new->add(42);