如何在 Moose 中定义默认属性属性值?
正如标题所示,我希望能够在我的类中执行类似的操作:
use MooseX::Declare;
class MyClass {
default_attribute_propeties(
is => 'ro',
lazy => 1,
required => 1,
);
has [qw( some standard props )] => ();
has 'override_default_props' => (
is => 'rw',
required => 0,
...
);
...
}
即定义一些默认属性值,这些值将应用于所有属性定义,除非被覆盖。
As the title suggests, I'd like to be able to do something like this in my class:
use MooseX::Declare;
class MyClass {
default_attribute_propeties(
is => 'ro',
lazy => 1,
required => 1,
);
has [qw( some standard props )] => ();
has 'override_default_props' => (
is => 'rw',
required => 0,
...
);
...
}
That is, define some default property values that will apply to all attribute definitions unless overridden.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您想编写一些自定义属性声明,提供一些默认选项。 Moose::Cookbook::Extending:: 对此进行了介绍Recipe1,例如:
然后在你的班级中:
It sounds like you want to write some custom attribute declarations, that provide some default options. This is covered in Moose::Cookbook::Extending::Recipe1, e.g.:
And then in your class: