Further to my previous answer, here's another way to do it. It pushes all the ugly out to an attribute trait.

use v5.14; use strict; use warnings; # The ugly lives in this package package MooseX::UnionInheritedTypeConstraint { use Moose::Role; use Moose::Util::TypeConstraints -all; use namespace::clean -except => ['meta']; around new => sub { my ($orig, $class, $name, %options) = @_; if (my $new = $options{isa}) { my $existing = $options{associated_class} -> find_attribute_by_name($name) -> type_constraint; if ($existing) { $new = Moose::Util::TypeConstraints::find_or_parse_typ +e_constraint($new) unless ref $new; $options{isa} = union([$existing, $new]); } } $class->_process_isa_option($name, \%options); # maybe need to process coerce too?? return $class->$orig($name, %options); }; } # No ugly below! package KeyAtom { use Moose; has data => ( is => 'rw', isa => 'Str | RegexpRef', ); } package ValAtom { use Moose; extends 'KeyAtom'; has '+data' => ( traits => [ 'MooseX::UnionInheritedTypeConstraint' ], isa => 'ArrayRef | HashRef', ); } ValAtom->new(data => 'Hello'); # Str ValAtom->new(data => qr{Hello}); # RegexpRef ValAtom->new(data => []); # ArrayRef ValAtom->new(data => {}); # HashRef ValAtom->new(data => \*STDOUT); # none of the above... crash!

You might notice that I've managed to avoid the dependency on MooseX::Types here; though the trait should work equally well if you're using MooseX::Types.

PS: You can actually see the code that Moose uses to check type constraints. This is sometimes handy...

my $tc = ValAtom->meta->get_attribute('data')->type_constraint; say $tc->_inline_check('$value') if $tc->can_be_inlined;
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

In reply to Re: Moose "unions" and inheritance by tobyink
in thread Moose "unions" and inheritance by PetaMem

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.