我已经创建了带有强制转换的子类型,如下所示,并且我尝试根据Moose::Manual::Types 将Birth_d其与内置类型结合使用。Maybe
我收到错误You cannot coerce an attribute (birth_d) unless its type (Maybe[Birth_d]) has a coercion。这是完整的测试代码:
package Student;
use Moose;
use Moose::Util::TypeConstraints;
use DateTime::Format::MySQL;
class_type 'Birth_d', { class => 'DateTime' };
coerce 'Birth_d',
from 'Str',
via { DateTime::Format::MySQL->parse_date( $_ ) };
has 'name' => (
isa => 'Str',
is => 'ro',
);
has 'birth_d' => (
isa => 'Maybe[Birth_d]', # This works: isa => 'Birth_d'
coerce => 1,
is => 'ro',
);
package main;
use Test::More;
my $student = Student->new(
name => 'Johnnie Appleseed',
birth_d => '2015-01-01'
);
is ( $student->birth_d->ymd(), '2015-01-01' );
my $student2 = Student->new(
name => 'Foo Bar',
birth_d => undef
);
is( $student2->birth_d, undef );
Run Code Online (Sandbox Code Playgroud)
替换isa => 'Maybe[Birth_d]'为isa => 'Birth_d'有效,但不是所需要的。我需要将birth_d设为可选,如果未提供,则应为undef。
我应该补充一点,我尝试使用 MooseX::Types 将这个 Birth_d 类型隐藏在一个单独的地方,但发现它对裸字的随意使用有点不正统,所以我慢慢地退缩了。如果有意义的话,我愿意重新考虑它。
Moose不会进行任何强制链接,换句话说,您必须明确告诉它如何转换为Maybe[Birth_d].
您可以通过重用现有的强制来做到这一点Birth_d:
package Student;
use Moose;
use Moose::Util::TypeConstraints;
use DateTime::Format::MySQL;
# save the Moose::Meta::TypeConstraint object
# you can also get it with find_type_constraint('Birth_d')
my $birth_d = class_type 'Birth_d', { class => 'DateTime' };
coerce 'Birth_d',
from 'Str',
via { DateTime::Format::MySQL->parse_date( $_ ) };
subtype 'MaybeBirth_d',
as 'Maybe[Birth_d]';
coerce 'Maybe[Birth_d]',
from 'Str|Undef',
via { $birth_d->coerce($_) };
has 'name' => (
isa => 'Str',
is => 'ro',
);
has 'birth_d' => (
isa => 'Maybe[Birth_d]',
coerce => 1,
is => 'ro',
predicate => 'has_birth_d', # as per your comment
);
package main;
use Test::More;
my $student = Student->new(
name => 'Johnnie Appleseed',
birth_d => '2015-01-01'
);
is ( $student->birth_d->ymd(), '2015-01-01' );
my $student2 = Student->new(
name => 'Foo Bar',
birth_d => undef
);
is( $student2->birth_d, undef );
ok( $student2->has_birth_d );
done_testing;
Run Code Online (Sandbox Code Playgroud)