:LazyInit
field $name :LazyInit(NAME) ...;
Declares that if the field variable is read from before it has been otherwise initialised, then the named
method will be called first to create an initial value for it. Initialisation by either by a ":param"
declaration, explicit assignment into it, or the use of a ":writer" accessor will set the value for the
field and mean the lazy initialiser will not be invoked.
After it has been invoked, the value is stored by the field and thereafter it will behave as normal;
subsequent reads will return the current value, and the initialiser method will not be invoked again.
In order to avoid the possibility of accidental recursion when generating the value, it is recommended
that the logic in the lazy initialisation method be as self-contained as possible; ideally not invoking
any methods on $self, and only making use of other fields already declared before the field being
initialised. By placing the initialiser method immediately after the field declaration, before any other
fields, you can reduce the possibility of getting stuck in such a manner.
field $field_zero :param;
field $field_one :LazyInit(_make_one);
method _make_one {
# we can safely use $field_zero in here
}
field $field_two :LazyInit(_make_two);
method _make_two {
# we can safely use $field_zero and $field_one
}