develooper Front page | perl.moose | Postings from June 2014

Re: coerce a sub-subtype

Thread Previous | Thread Next
Chris Weyl
June 16, 2014 19:25
Re: coerce a sub-subtype
Message ID:
On Mon, Jun 16, 2014 at 11:41 AM, John Macdonald <>

> without the coerce, it does not do the conversion and only allows input
> that has the explicit leading 'chr', but with the coerce parameter it gives
> a compile time error complaining that the type needs to have a coercion.

By design, coercions are not inherited.  (Not making a value assessment
here, just confirming your (inadvertent) research :))

 Am I going to have to write the duplicate coercion code for every one of
> the 'real' sub-types that want to also have this optional prefix behaviour,
> or is there an alternate way to write this?

tl;dr: yes :)

Alternatively, it's possible to do a little type metaclass fiddling to find
the parent type's coercions and pull those into the child's set.  This is
what we do in MooseX::AttributeShortcuts, for instance, when using the
one-off subtyping behaviour.


Chris Weyl
Ex astris scientia

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About