我可以使用基于 Moose 的对象将正则表达式传递给 isa() 吗?

发布于 2024-08-25 05:17:19 字数 250 浏览 9 评论 0原文

我可以在 Moose 中使用 isa 并以正则表达式作为参数吗?如果不可能,我可以使用 ->isa 以外的其他方法实现相同的效果吗?

好的,有以下类型 Animal::GiraffeAnimal::Carnivore::Crocodile ,我想做 ->isa(/^Animal:: /),我可以这样做吗?如果不能,我可以用什么来达到预期的效果?

Can I use isa in Moose with a regex as a parameter ? If not possible can I achieve the same thing with someothing other than ->isa ?

ok, having the following types Animal::Giraffe , Animal::Carnivore::Crocodile , I want to do ->isa(/^Animal::/), can I do that ? if I can't, what can I use to reach the desired effect ?

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

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

发布评论

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

评论(4

┾廆蒐ゝ 2024-09-01 05:17:19

这些相关类型应该都“扮演”相同的角色,动物。然后你可以这样写:

has 'animal' => (
    is       => 'ro',
    does     => 'Animal',
    required => 1,
);

现在你有了比正则表达式更可靠的东西来确保程序的一致性。

These related types should all "do" the same role, Animal. Then you can write:

has 'animal' => (
    is       => 'ro',
    does     => 'Animal',
    required => 1,
);

Now you have something much more reliable than a regex to ensure the consistency of your program.

一张白纸 2024-09-01 05:17:19

Leon Timmermans 的答案接近我的建议,尽管我会使用 Moose::Util::TypeConstraints 中的糖

use Moose;
use Moose::Util::TypeConstraints;

subtype Animal => as Object => where { blessed $_ =~ /^Animal::/ };

has animal => ( is => 'rw', isa => 'Animal' );

Leon Timmermans' answer was close to what I'd suggest though I'd use the sugar from Moose::Util::TypeConstraints

use Moose;
use Moose::Util::TypeConstraints;

subtype Animal => as Object => where { blessed $_ =~ /^Animal::/ };

has animal => ( is => 'rw', isa => 'Animal' );
颜漓半夏 2024-09-01 05:17:19

扩展 perigrin 的答案,以便如果该类在其超类中的任何位置都有 Animal::* ,而不仅仅是在其直接类名称中(例如,如果 Helper::Monkey code> isa Animal::Monkey):

use Moose;
use Moose::Util::TypeConstraints;

subtype Animal => 
  as Object =>
  where { grep /^Animal::/, $_->meta->linearized_isa };

has animal => ( is => 'rw', isa => 'Animal' );

我认为 jrockway 使用角色的建议有很多优点,但如果你想这样做,你最好涵盖所有基础。

Extending perigrin's answer so that it will work if the class has an Animal::* anywhere in its superclasses, and not only in its immediate class name (for example if Helper::Monkey isa Animal::Monkey):

use Moose;
use Moose::Util::TypeConstraints;

subtype Animal => 
  as Object =>
  where { grep /^Animal::/, $_->meta->linearized_isa };

has animal => ( is => 'rw', isa => 'Animal' );

I think jrockway's suggestion to use a role instead has a lot of merit, but if you want to go this way instead, you might as well cover all of the bases.

何处潇湘 2024-09-01 05:17:19

我认为这应该可以做到。

use Moose;
use Moose::Util::TypeConstraints;

my $animal = Moose::Meta::TypeConstraint->new(
    constraint => sub { $_[0] =~ /^Animal::/}
);

has animal => (is => 'rw', isa => $animal);

ETA:不过我同意 jrockway 的观点:除非你有令人信服的理由,否则你应该只使用继承。

I think this should do it.

use Moose;
use Moose::Util::TypeConstraints;

my $animal = Moose::Meta::TypeConstraint->new(
    constraint => sub { $_[0] =~ /^Animal::/}
);

has animal => (is => 'rw', isa => $animal);

ETA: I agree with jrockway though: unless you have a convincing reason otherwise, you should just use inheritance.

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