[英]Do MooseX::AttributeHelpers and MooseX::FollowPBP interact correctly?
The following code defines two classes ( DeckA
and DeckB
) that differ only in whether they use the features that come with MooseX::AttributeHelpers . 以下代码定义了两个类(
DeckA
和DeckB
),它们的区别仅在于它们是否使用MooseX :: AttributeHelpers随附的功能。 The getters generated by Moose for DeckB
are not what I expected. Moose为
DeckB
生成的吸气剂不是我所期望的。 Is this a bug or am I misunderstanding how MooseX::AttributeHelpers and MooseX::FollowPBP ought to interact? 这是一个错误还是我误解了MooseX :: AttributeHelpers和MooseX :: FollowPBP应该如何交互?
My workaround for now has been to avoid using the is
argument in such situations and instead declare a reader
and writer
as needed. 我现在的解决方法是避免在这种情况下使用
is
参数,而是根据需要声明reader
和writer
reader
。
use strict;
use warnings;
my %moose_args = (
isa => 'ArrayRef[Str]',
is => 'ro',
default => sub {[]},
);
my %moose_attr_helper_args = (
metaclass => 'Collection::Array',
provides => {
elements => 'get_all_cards',
},
);
package DeckA;
use Moose;
use MooseX::FollowPBP;
use MooseX::AttributeHelpers;
has 'cards' => (%moose_args);
package DeckB;
use Moose;
use MooseX::FollowPBP;
use MooseX::AttributeHelpers;
has 'cards' => (%moose_args, %moose_attr_helper_args);
package main;
for my $class (qw(DeckA DeckB)){
my $deck = $class->new;
print "\n$class\n";
for my $method ( qw(cards get_cards get_all_cards) ){
print "$method: ", $deck->can($method) ? 'yes' : 'no', "\n";
}
}
Output: 输出:
DeckA
cards: no
get_cards: yes
get_all_cards: no
DeckB
cards: yes # Not what I expected.
get_cards: no # Not what I expected.
get_all_cards: yes
They don't work when you use the metaclass option for MX::AH. 当您为MX :: AH使用metaclass选项时,它们不起作用。
However, the latest Moose has integrated support for native helpers, with a slightly tweaked API. 但是,最新的Moose通过稍微调整的API集成了对本机帮助程序的支持。 This version uses traits (a role applied to the attribute), and it should work just fine with MX::FollowPBP.
此版本使用特征(应用于属性的角色),并且与MX :: FollowPBP一起使用应该可以正常工作。
I had the same problem, so I really appreciate FM's question and Dave Rolsky's answer. 我遇到了同样的问题,因此我非常感谢FM的问题和Dave Rolsky的回答。
Rephrasing part of his answer so that my simple self would have understood it the first time I read it: 重述他的部分答案,以便我第一次阅读时简单的自我就会理解:
Instead of using MooseX::AttributeHelpers, you can simply use "traits" in the latest version of Moose.
除了使用MooseX :: AttributeHelpers外,您还可以在最新版本的Moose中使用“特征”。 This eliminates the conflict with MooseX::FollowPBP, while still giving you the same functionality.
这消除了与MooseX :: FollowPBP的冲突,同时仍为您提供了相同的功能。
For using traits, see Moose::Meta::Attribute::Native.
有关使用特征的信息,请参见Moose :: Meta :: Attribute :: Native。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.