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

RE: coerce a sub-subtype

Thread Previous | Thread Next
John Macdonald
June 16, 2014 19:41
RE: coerce a sub-subtype
Message ID:
I'm more likely to make mistakes and/or get variations in the code using and maintaining the metaclass fiddling than I would from just copying the coercion - so it looks like I get to re-write that coercion for every variant class.  In this particular case, I'm not likely to need to fix the code of the coercion - so having it cut-and-paste duplicated multiple times is not much of a hazard.

In general, though, having a way of inheriting subtypes could be potentially a big saving in this sort of case.  (Not so much for the number of lines of code that are saved, of course, but for the ability to make a fix by editting one instance of code rather than having to find *all* of the copies and edit them all without losing focus and making mistakes.  "If there is more than one copy of code, at most one of them is right after a significant amount of time passes.")

I had an off-line suggestion to look at Type::Tiny and/or Specio - they may be a better alternative for this sort of situation.

John Macdonald
Software Engineer

Ontario Institute for Cancer Research
MaRS Centre

661 University Avenue

Suite 510
Toronto, Ontario

Canada M5G 0A3



Toll-free: 1-866-678-6427
Twitter: @OICR_news<>

This message and any attachments may contain confidential and/or privileged information for the sole use of the intended recipient. Any review or distribution by anyone other than the person for whom it was originally intended is strictly prohibited. If you have received this message in error, please contact the sender and delete all copies. Opinions, conclusions or other information contained in this message may not be that of the organization.

From: [] on behalf of Chris Weyl []
Sent: June 16, 2014 3:25 PM
To: John Macdonald
Subject: Re: coerce a sub-subtype

On Mon, Jun 16, 2014 at 11:41 AM, John Macdonald <<>> wrote:
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