I can do this:
package Foo;
use Moose;
has 'time' => (
is => 'rw',
isa => 'DateTime'
);
package main;
use DateTime;
my $a = Foo->new(time => DateTime->now);
But not this:
package Foo;
use Moose;
has 'time' => (
is => 'rw',
isa => 'DateTime | Str'
);
package main;
use DateTime;
my $a = Foo->new(time => DateTime->now);
As it raises an exception:
Could not locate type constraint (DateTime) for the union
at /opt/xt/xt-perl/lib/site_perl/5.8.8/Moose/Util/TypeConstraints.pm line 89
Without defining a SubType first. Why is this, and is there a way around it (apart from defining a subtype that checks 'isa')?
When Moose creates a type union it has to know all the components of the union. In this case, it does not yet know the DateTime type. However when you create an attribute in Moose and Moose does not recognize the type, it makes an assumption that you want a class-type of the string in isa
and just does that. You can solve you issue by simply loading Moose::Util::TypeConstraints and doing
class_type 'DateTime';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With