在 Perl/Moose 中,我可以拥有两个具有相互依赖的默认值的属性吗?

发布于 2024-10-16 23:43:20 字数 421 浏览 7 评论 0原文

我可以在穆斯做这个吗?

package SomeClass;
use Moose;

has start => (
    isa => 'Int',
    is => 'ro',
    lazy => 1,
    default => sub { $_[0]->end },
);

has end => (
    isa => 'Int',
    is => 'ro',
    lazy => 1,
    default => sub { $_[0]->start },
);

...

换句话说,我想要两个名为“start”和“end”的属性,如果只指定其中一个,我希望将另一个设置为相同的值。不指定其中任何一个都是错误的。

这种相互依赖的设置有效吗?

Can I do this in Moose?

package SomeClass;
use Moose;

has start => (
    isa => 'Int',
    is => 'ro',
    lazy => 1,
    default => sub { $_[0]->end },
);

has end => (
    isa => 'Int',
    is => 'ro',
    lazy => 1,
    default => sub { $_[0]->start },
);

...

In other words, I want two attributes called "start" and "end", and if only one of them is specified, I want the other one to be set to the same thing. Not specifying either one is an error.

Does this mutually-dependent setup work?

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

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

发布评论

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

评论(2

时光病人 2024-10-23 23:43:20

是的,如果您通过验证至少指定了这些值之一来消除无限递归的可能性:

has start => (
    ...
    predicate => 'has_start',
);

has end => (
    ...
    predicate => 'has_end',
);

sub BUILD
{
    my $self = shift;

    die "Need to specify at least one of 'start', 'end'!" if not $self->has_start and not $self->has_end;
}

或者,您可以延迟对默认子项的检查:

has start => (
    ...
    predicate => 'has_start',
    default => sub {
        my $self = shift;
        die "Need to specify at least one of 'start', 'end'!" if not $self->has_end;
        $self->end;
    },
);

has end => (
    ...
    predicate => 'has_end',
    default => sub {
        my $self = shift;
        die "Need to specify at least one of 'start', 'end'!" if not $self->has_start;
        $self->start;
    },
);

Yes, if you remove the possibility of infinite recursion by verifying that at least one of these values is specified:

has start => (
    ...
    predicate => 'has_start',
);

has end => (
    ...
    predicate => 'has_end',
);

sub BUILD
{
    my $self = shift;

    die "Need to specify at least one of 'start', 'end'!" if not $self->has_start and not $self->has_end;
}

Alternatively, you could delay the check to the default subs:

has start => (
    ...
    predicate => 'has_start',
    default => sub {
        my $self = shift;
        die "Need to specify at least one of 'start', 'end'!" if not $self->has_end;
        $self->end;
    },
);

has end => (
    ...
    predicate => 'has_end',
    default => sub {
        my $self = shift;
        die "Need to specify at least one of 'start', 'end'!" if not $self->has_start;
        $self->start;
    },
);
三寸金莲 2024-10-23 23:43:20

就我个人而言,我会利用惰性来确保我不会陷入无限递归:

has start => (
  is => 'ro',
  isa => 'Int',
  lazy => 1,
  default => sub { shift->end },
  predicate => 'has_start',
);

has end => (
  is => 'ro',
  isa => 'Int',
  lazy => 1,
  default => sub { shift->start },
  predicate => 'has_end',
);

sub BUILD {
  my $self = shift;

  die "Need to specify at least one of 'start', 'end'!" 
    unless $self->has_start || $self->has_end;
}

Personally, I'd take advantage of laziness to ensure that I didn't get caught in an infinite recursion:

has start => (
  is => 'ro',
  isa => 'Int',
  lazy => 1,
  default => sub { shift->end },
  predicate => 'has_start',
);

has end => (
  is => 'ro',
  isa => 'Int',
  lazy => 1,
  default => sub { shift->start },
  predicate => 'has_end',
);

sub BUILD {
  my $self = shift;

  die "Need to specify at least one of 'start', 'end'!" 
    unless $self->has_start || $self->has_end;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文