[ Reported in https://rt.cpan.org/Ticket/Display.html?id=58387. ]

If you care to file a bug report or add the missing functionality yourself, here's a test case:

BEGIN { package Class; use strict; use warnings; use Moose; has val => ( is => 'rw', isa => 'Num', required => 1, ); use Moose::Util::TypeConstraints; coerce __PACKAGE__ => from 'Num' => via { __PACKAGE__->new(val => $_) }; } BEGIN { package Container; use strict; use warnings; use Moose; has always => ( is => 'rw', isa => 'Class', coerce => 1, default => sub { Class->new(val => 0) }, ); has maybe => ( is => 'rw', isa => 'Maybe[Class]', coerce => 1, default => undef, ); } use strict; use warnings; use Test::More tests => 15; my $always_fail = qr/^Attribute \(always\) does not pass the type cons +traint/; my $maybe_fail = qr/^Attribute \(maybe\) does not pass the type const +raint/; my $o = Class->new(val => 123); eval { Container->new() }; is($@, ''); eval { Container->new(always => $o) }; is($@, ''); eval { Container->new(always => 123) }; is($@, ''); eval { Container->new(always => "abc") }; like($@, $always_fail); eval { Container->new(maybe => undef) }; is($@, ''); eval { Container->new(maybe => $o) }; is($@, ''); eval { Container->new(maybe => 123) }; is($@, ''); eval { Container->new(maybe => "abc") }; like($@, $maybe_fail); my $c = Container->new(); eval { $c->always($o) }; is($@, ''); eval { $c->always(123) }; is($@, ''); eval { $c->always("abc") }; like($@, $always_fail); eval { $c->maybe(undef) }; is($@, ''); eval { $c->maybe($o) }; is($@, ''); eval { $c->maybe(123) }; is($@, ''); eval { $c->maybe("abc") }; like($@, $maybe_fail);

Currently failing:

eval { Container->new(maybe => 123) }; is($@, ''); eval { $c->maybe(undef) }; is($@, ''); eval { $c->maybe($o) }; is($@, ''); eval { $c->maybe(123) }; is($@, ''); eval { $c->maybe("abc") }; like($@, $maybe_fail);

In reply to Re^3: Moose type question by ikegami
in thread Moose type question by Sue D. Nymme

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.