将受祝福的 CodeRefs 与 Moose 类型约束一起使用
我们使用 Moose 类将迭代器序列化为各种输出格式。我们将迭代器描述为一个属性:
has iterator => (
is => 'ro',
isa => 'CodeRef',
required => 1,
);
到目前为止,这工作得很好,但我们最近一直在使用 Iterator::Simple 准备迭代器以供以后使用。这意味着我们可以继续编写以下内容:
has iterator => (
is => 'ro',
isa => 'CodeRef|Iterator::Simple::Iterator',
required => 1,
);
并允许我们的序列化器正确接受迭代器类。然而,这似乎是一个不完整的解决方案。
Moose 有没有办法指定属性必须可调用的约束?我怀疑 Moose::Util::TypeConstraints 并在 &{}
上使用 overload::Overloaded 进行检查,但我想了解是否有人已经创建了一个模块来执行此操作,或者是否有 Moose 标准方法来测试此操作。
We use Moose classes that serialize iterators into various output formats. We describe the iterator as an attribute:
has iterator => (
is => 'ro',
isa => 'CodeRef',
required => 1,
);
This has worked fine so far, but we have lately been using Iterator::Simple to prepare iterators for later consumption. This means that we can go about writing this:
has iterator => (
is => 'ro',
isa => 'CodeRef|Iterator::Simple::Iterator',
required => 1,
);
And allow our serializers to accept the iterator class correctly. However, that seems to be a incomplete solution.
Is there a way in Moose to specify the constraint that the attribute must be callable? I suspect it may be possible with Moose::Util::TypeConstraints and using overload::Overloaded on &{}
to check, but I'd like to know if anyone has created a module to do this already or if there is a Moose-standard way to test for this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
CodeRef
只允许未受祝福的代码引用。幸运的是,创建自己的类型很容易。如下所示定义
Callable
,然后使用它代替CodeRef
。它允许以下操作:&{}
的对象)。CodeRef
only allows unblessed code references. Fortunately, it's easy to make your own types.Define
Callable
as shown below, then use it instead ofCodeRef
. It allows the following:&{}
).您知道如何从 Scalar::Util::reftype 获取足够的信息吗?
Do you know enough to get from Scalar::Util::reftype?