Moose:重写派生类中属性的“必需”性

发布于 2024-11-14 02:09:13 字数 1755 浏览 5 评论 0原文

我有一个基类和十几个派生类。除一个派生类外,所有派生类都需要名为 key 的属性。所以我可以将它添加到十一个派生类中,而保留第十二个类。

然而,出于惰性,我想将属性添加到基类中,从而避免重复声明十一次,并添加我认为的一致性和简单性。

现在,这给不需要 key 属性的一个类带来了问题。请注意,如果此类具有此属性,但它不需要它,也没有什么坏处。

我的想法是通过使用标志方法 is_strict 来解决这个问题,该方法将从 BUILDARGS 调用来决定是否需要 key 。这是一个简单的脚本来说明这一点(好吧,我颠倒了概念,仅在一种情况下需要 key 属性(而不是除一种情况外的所有情况),但问题仍然受到这种倒置的影响) :

#!perl
package Bla;
use Moose;
use Carp ();
has grop => is => 'ro', isa => 'Str'; # optional
has key  => is => 'ro', isa => 'Int'; # required in all but one cases
# required => should depend on $class->is_strict;
sub is_strict { 0 } # not strict by default as per this base class
# imagine a bunch of other stuff here shared by all derived classes
around BUILDARGS => sub {
    my $orig = shift;
    my $class = shift;
    my $args = @_ == 1 ? shift : { @_ };
    Carp::croak 'key missing'
        if not exists $args->{key}
        and $class->is_strict;
    return $class->$orig( @_ );
};
no Moose; __PACKAGE__->meta->make_immutable;

package Bla::Eins;
use Moose; extends 'Bla';
no Moose; __PACKAGE__->meta->make_immutable;

package Bla::Zwei;
use Moose; extends 'Bla';
no Moose; __PACKAGE__->meta->make_immutable;

package Bla::Drei;
use Moose; extends 'Bla';
override is_strict => sub { 1 }; # but here it is required
no Moose; __PACKAGE__->meta->make_immutable;

package main;
use Test::More;
use Test::Exception;
lives_ok  { Bla::Eins->new };
lives_ok  { Bla::Zwei->new };
throws_ok { Bla::Drei->new } qr/key missing/;
lives_ok  { Bla::Drei->new( key => 99 ) };
done_testing;

这可行,但是有更好的方法来实现我想要的吗?

I have a base class and a dozen derived classes. All but one derived classes require an attribute named key. So I could add it to eleven derived classes and leave the twelfth one alone.

However, laziness being what it is, I'd like to add the attribute to the base class, thus avoiding repeating the declaration eleven times and adding what I consider is consistency and simplicity.

Now, that poses a problem for the one class that does not require the key attribute. Note that there is no harm if this class has this attribute, but it does not require it.

My idea has been to resolve this by using a flag method is_strict, which would be called from BUILDARGS to decide whether the key is required or not. Here's a simple script to illustrate this (okay, I inverted the notion, the key attribute is required in only one case (instead of in all but one cases), but the problem remains affected by this inversion):

#!perl
package Bla;
use Moose;
use Carp ();
has grop => is => 'ro', isa => 'Str'; # optional
has key  => is => 'ro', isa => 'Int'; # required in all but one cases
# required => should depend on $class->is_strict;
sub is_strict { 0 } # not strict by default as per this base class
# imagine a bunch of other stuff here shared by all derived classes
around BUILDARGS => sub {
    my $orig = shift;
    my $class = shift;
    my $args = @_ == 1 ? shift : { @_ };
    Carp::croak 'key missing'
        if not exists $args->{key}
        and $class->is_strict;
    return $class->$orig( @_ );
};
no Moose; __PACKAGE__->meta->make_immutable;

package Bla::Eins;
use Moose; extends 'Bla';
no Moose; __PACKAGE__->meta->make_immutable;

package Bla::Zwei;
use Moose; extends 'Bla';
no Moose; __PACKAGE__->meta->make_immutable;

package Bla::Drei;
use Moose; extends 'Bla';
override is_strict => sub { 1 }; # but here it is required
no Moose; __PACKAGE__->meta->make_immutable;

package main;
use Test::More;
use Test::Exception;
lives_ok  { Bla::Eins->new };
lives_ok  { Bla::Zwei->new };
throws_ok { Bla::Drei->new } qr/key missing/;
lives_ok  { Bla::Drei->new( key => 99 ) };
done_testing;

This works, but is there a better way to achieve what I want?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

决绝 2024-11-21 02:09:13

好吧,我有点愚蠢,因为没有尝试最明显的解决方案,只需重写派生类中的属性定义,其中所需的属性与默认值不同。开始吧:

#!perl
package Bla;
use Moose;
use Carp ();
has grop => is => 'ro', isa => 'Str'; # optional
has key  => is => 'ro', isa => 'Int'; # required in all but one cases
# imagine a bunch of other stuff here shared by all derived classes
no Moose; __PACKAGE__->meta->make_immutable;

package Bla::Eins;
use Moose; extends 'Bla';
no Moose; __PACKAGE__->meta->make_immutable;

package Bla::Zwei;
use Moose; extends 'Bla';
no Moose; __PACKAGE__->meta->make_immutable;

package Bla::Drei;
use Moose; extends 'Bla';
# prefix an attribute you're overriding with a "+" sign
has '+key' => is => 'ro', isa => 'Int', required => 1;
no Moose; __PACKAGE__->meta->make_immutable;

package main;
use Test::More;
use Test::Exception;
lives_ok  { Bla::Eins->new };
lives_ok  { Bla::Zwei->new };
throws_ok { Bla::Drei->new } qr/\bkey\b.*\brequired\b/;
lives_ok  { Bla::Drei->new( key => 99 ) };
done_testing;

不过,我还是很感谢所有的反馈,因为这个 Mooseland 的章程不如其他语言的对象系统那么好。

啊,我已经看到你应该在你要覆盖的属性定义前面加上 + ,所以如果引用的属性不是',Moose 会可怜地呱呱 t 在父类中找到,增加了代码的安全性和一致性。我已经相应地更新了我的示例代码。

Okay, I've been a little dumb for not having tried the most obvious solution, just override the attribute definition in the derived class where the requiredness differs from the default. Here we go:

#!perl
package Bla;
use Moose;
use Carp ();
has grop => is => 'ro', isa => 'Str'; # optional
has key  => is => 'ro', isa => 'Int'; # required in all but one cases
# imagine a bunch of other stuff here shared by all derived classes
no Moose; __PACKAGE__->meta->make_immutable;

package Bla::Eins;
use Moose; extends 'Bla';
no Moose; __PACKAGE__->meta->make_immutable;

package Bla::Zwei;
use Moose; extends 'Bla';
no Moose; __PACKAGE__->meta->make_immutable;

package Bla::Drei;
use Moose; extends 'Bla';
# prefix an attribute you're overriding with a "+" sign
has '+key' => is => 'ro', isa => 'Int', required => 1;
no Moose; __PACKAGE__->meta->make_immutable;

package main;
use Test::More;
use Test::Exception;
lives_ok  { Bla::Eins->new };
lives_ok  { Bla::Zwei->new };
throws_ok { Bla::Drei->new } qr/\bkey\b.*\brequired\b/;
lives_ok  { Bla::Drei->new( key => 99 ) };
done_testing;

Still, I'm thankful for all feedback as this Mooseland is less well chartered than object systems in other languages.

Ah, and I've seen you're supposed to prefix an attribute definition that you're overriding with a +, so Moose will pitifully croak if the referenced attribute isn't found in a parent class, adding security and consistency to your code. I've updated my sample code accordingly.

反差帅 2024-11-21 02:09:13

而不是使用 has '+key' => (requires => 1),您可以将 requires=>; 1 原文中使用 has '+key' => (需要 => 0) 将其关闭。在最初的请求中,有 11 个是必需的,只有一个不是必需的,因此这意味着只有一个定义需要覆盖,其他 11 个定义可以保持不变。

Rather than using has '+key' => (requires => 1), you could put the requires=> 1 in the original and use has '+key' => (requires => 0) to turn it off. In the original request, there were 11 required and only one not-required, so that would mean only one definition needs the over-ride and the other 11 can take it unchanged.

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