【问题标题】:What's the order of construction of an extend-ing Moose object class?扩展 Moose 对象类的构造顺序是什么?
【发布时间】:2010-11-12 18:00:10
【问题描述】:

我写了一个 Moose 对象类,它扩展了另一个 Moose 对象类:

package MySubClass;
use Moose;
extends MySuperClass;

我想在对象实例化的基础上自动构建一个属性:

has 'id' => (
    is       => 'ro',
    isa      => 'Str',
    builder  => '_build_id',
    init_arg => undef,
); 

sub _build_id {
 my $self = shift;
 # both ssn and bnn are attributes of MySuperClass
 return $self->ssn . $self->bnn;
}

除非我将id 设置为惰性,否则这不起作用。为什么?

extending 对象的构造是如何完成的?

【问题讨论】:

    标签: perl moose


    【解决方案1】:

    引用Moose::Manual::Attributes on Laziness

    首先,如果这个的默认值 属性取决于其他一些 属性,那么属性必须是 懒惰的。在对象构造过程中, 默认值不是在 可预测的顺序,所以你无法计算 在其他一些属性上 生成默认值时填充。

    【讨论】:

      猜你喜欢
      • 2020-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-16
      • 2021-08-20
      • 2020-06-12
      相关资源
      最近更新 更多