Skip Menu |
 

This queue is for tickets about the Moose CPAN distribution.

Report information
The Basics
Id: 73764
Status: stalled
Priority: 0/
Queue: Moose

People
Owner: Nobody in particular
Requestors:
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: 2.0401
Fixed in: (no value)



Subject: attribute coerce with deep type doesn't see subtype coercions
Download (untitled) / with headers
text/plain 449b
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
Download arrayref_coerce.pl
text/x-perl 364b
#!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);
Download (untitled) / with headers
text/plain 579b
To clarify, and quoting doy: the issue is just that you can't do "has foo => (is => 'ro', isa => 'ArrayRef[Foo]', coerce => 1, handles => { push => 'push' }) if there is only a coercion on Foo --- The issue is purely that: coerce => 1 is required on the attribute for the coercion to work on the items pushed/pulled. coerce => 1 requires that a clear coercion is seen for the type constraint. IM*V*HO, this could be as simple as saying that coerce is the right thing to use, but it should see that there's a nested type and check for coercions on the nested type itself.
Download (untitled) / with headers
text/plain 372b
For future bug archeoligsts ... I don't think we will ever fix this. Coercions are not deep, and implementing that might be too complicated (and too likely to break existing code). The "simple" fix is to create a new type like "MyArrayOfInt" and define a coercion on that type from the appropriate things. This can get tedious, but it's very predictable and unambiguous.


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

Please report any issues with rt.cpan.org to rt-cpan-admin@bestpractical.com.