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

Re: Moose Type Constraints violations to warnings?

Thread Previous | Thread Next
From:
Chris Weyl
Date:
June 11, 2011 11:22
Subject:
Re: Moose Type Constraints violations to warnings?
Message ID:
BANLkTi=PxwXKe8FjkSw6zHJYun-JnGknsg@mail.gmail.com
On Fri, Jun 10, 2011 at 3:06 AM, Ovid <publiustemp-catalyst@yahoo.com>wrote:

> Without using signal handlers, is there some way I can convert that
> exception into a warning except when something like $ENV{HARNESS_ACTIVE} is
> true? Preferably on a per-attribute basis?


With the caveat that I haven't checked for any larger implications, and I'm
not making any value judgements here as to the wisdom of selectively
disabling the tc mechanism:

This could be done in Moose by creating and selectively applying an
attribute trait that overrides the TC inline code generation methods
(_inline_tc_code(), _inline_check_constraint(), etc) and others related to
enforcing the type constraints.  You may also need a constructor trait for
your class' metaclass that tells it to not explode when it hits a tc
violation.  Moose itself provides the tools to make this a little easier;
see, e.g. MooseX::AutoDestruct for an example of traits that do this sort of
thing.</shamelessplug>

Not being terribly familiar with Mouse (except that one last year that ate a
perfectly good bag of rice), I'm not sure how to do something like this over
there.

I think Edward/Karen are on the right track, though:  If it's reasonable to
do so, building out a more tolerant type library with complimentary
coercions that help "correct" the data (or at least are broad enough to
never be violated) is probably more useful to you in the long run than
altering the tc system.  I'm not in any position to be making that judgement
for you, however, and an attribute trait to modify the tc behaviour
certainly looks to be feasible.

                                      -Chris

-- 
Chris Weyl
Ex astris scientia

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About