类型联合上的驼鹿特征
在 Moose v1.x 中,我曾经能够执行此操作:
package Class;
use Test::More tests => 1;
use Moose;
use MooseX::Types::Moose qw/Undef Str/;
eval {
has 'trait_boom' => (
is => 'rw'
, isa => Str | Undef
, default => ''
, traits => ['String']
);
};
ok ( !$@, "Created attr trait_boom, a type union of Str and Undef\n$@" );
但是,它不再适用于 Moose 2.x。我认为这个是一个错误。为什么 Moose 破坏了向后兼容性?还有其他方法可以完成这项工作吗?我希望它是 Undef
或 Str
。不过,我不想将 Undef
强制为空字符串。
我只是在这里问因为显然磁铁坏了
17:43 [perl2] -!- ERROR Closing Link: 64.200.109.13 (Banned)
In Moose v1.x, I used to be able to do this:
package Class;
use Test::More tests => 1;
use Moose;
use MooseX::Types::Moose qw/Undef Str/;
eval {
has 'trait_boom' => (
is => 'rw'
, isa => Str | Undef
, default => ''
, traits => ['String']
);
};
ok ( !$@, "Created attr trait_boom, a type union of Str and Undef\n$@" );
However, it no longer works with Moose 2.x. I assume this is a bug. Why did Moose break backwards compatibility? Is there another way to get this job done. I want that to be either Undef
or a Str
. I do not want to coerce Undef
to an empty string though.
I'm only asking here because apparently magnet is broke
17:43 [perl2] -!- ERROR Closing Link: 64.200.109.13 (Banned)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜这在 Moose 2.0300(2011 年 9 月 23 日星期五)中已更改:
您是否尝试过
Maybe[Str]
而不是Str |未定义?
I would guess this was changed in Moose 2.0300, Fri, Sep 23, 2011:
Have you tried
Maybe[Str]
instead ofStr | Undef
?正如我们在报告您规避禁令后立即在 MagNet 上告诉您的那样,这不是一个错误。该特征的方法不应该对 Undef 值起作用,因此允许此行为在 1.x 中工作是一个错误。 Moose 有针对正确行为进行优化的方法,并且从未承诺版本之间的错误兼容性。
您要么需要编写自己的特征,要么手动编写方法来处理这种情况。
As we told you on MagNet right after I reported you for ban evasion, this is not a bug. The trait's methods should never have worked against the value Undef, so allowing this behavior to work in 1.x was the bug. Moose has ways optimized for correct behavior and never promised bug compat between versions.
You will either need to write your own traits or write the methods by hand to deal with this situation.