我可以使用基于 Moose 的对象将正则表达式传递给 isa() 吗?
我可以在 Moose 中使用 isa 并以正则表达式作为参数吗?如果不可能,我可以使用 ->isa
以外的其他方法实现相同的效果吗?
好的,有以下类型 Animal::Giraffe
, Animal::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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这些相关类型应该都“扮演”相同的角色,动物。然后你可以这样写:
现在你有了比正则表达式更可靠的东西来确保程序的一致性。
These related types should all "do" the same role, Animal. Then you can write:
Now you have something much more reliable than a regex to ensure the consistency of your program.
Leon Timmermans 的答案接近我的建议,尽管我会使用 Moose::Util::TypeConstraints 中的糖
Leon Timmermans' answer was close to what I'd suggest though I'd use the sugar from Moose::Util::TypeConstraints
扩展 perigrin 的答案,以便如果该类在其超类中的任何位置都有
Animal::*
,而不仅仅是在其直接类名称中(例如,如果Helper::Monkey
code> isaAnimal::Monkey
):我认为 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 ifHelper::Monkey
isaAnimal::Monkey
):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.
我认为这应该可以做到。
ETA:不过我同意 jrockway 的观点:除非你有令人信服的理由,否则你应该只使用继承。
I think this should do it.
ETA: I agree with jrockway though: unless you have a convincing reason otherwise, you should just use inheritance.