None of the Moose built-in types treat overloaded objects as the non-object types they overload.
So for example, an object overloading "" will not pass the Str type constraint. And objects overloading 0+ will not pass Num or Int.
Whether you think this is a good design decision or not, it's the way Moose works, and changing it now would likely break a lot of stuff that relies on the existing behaviour.
Luckily, you don't need to use the built-in Moose Bool type. You can define your own type constraints.
Moose::Util::TypeConstraints makes this possible. MooseX::Types makes it a bit easier. But (and I'm biased on this!), I'd recommend using Types::Standard which is a type constraint library that works with Moose, Mouse, and Moo, but doesn't require any of them. Here's one way you could achieve your aims with it:
{ package My::Package; use Moose; use Types::Standard qw( Bool Overload ); has attr => ( is => 'ro', isa => Bool | Overload["bool"], ); __PACKAGE__->meta->make_immutable; }
Or even:
{ package My::Package; use Moose; use Types::Standard qw( Bool Overload ); has attr => ( is => 'ro', isa => Bool->plus_coercions( Overload["bool"], sub { !!$_ +} ), coerce => 1, ); __PACKAGE__->meta->make_immutable; }
In reply to Re: Cpanel::JSON::XS::true and Moose don't play well together
by tobyink
in thread Cpanel::JSON::XS::true and Moose don't play well together
by choroba
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |