类型联合上的驼鹿特征

发布于 2024-12-20 19:11:51 字数 715 浏览 0 评论 0原文

在 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 破坏了向后兼容性?还有其他方法可以完成这项工作吗?我希望它是 UndefStr。不过,我不想将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫卆 2024-12-27 19:11:51

我猜这在 Moose 2.0300(2011 年 9 月 23 日星期五)中已更改:

  • ->is_subtype_of->is_a_type_of 方法已更改其行为
    对于联合类型。以前,如果他们的任何成员都返回 true
    types 对于给定类型返回 true。现在,所有成员类型都必须
    返回真。 RT #67731。 (戴夫·罗尔斯基)

您是否尝试过 Maybe[Str] 而不是 Str |未定义?

I would guess this was changed in Moose 2.0300, Fri, Sep 23, 2011:

  • The ->is_subtype_of and ->is_a_type_of methods have changed their behavior
    for union types. Previously, they returned true if any of their member
    types returned true for a given type. Now, all of the member types must
    return true. RT #67731. (Dave Rolsky)

Have you tried Maybe[Str] instead of Str | Undef?

毅然前行 2024-12-27 19:11:51

正如我们在报告您规避禁令后立即在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文