Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can I use a class name as a Moose type, but not when part of a type union?

Tags:

perl

moose

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')?

like image 363
cubabit Avatar asked Mar 04 '11 16:03

cubabit


1 Answers

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';
like image 169
Stevan Little Avatar answered Nov 05 '22 13:11

Stevan Little