Lazy
NAME
Attribute::Lazy - lazy attribute initialisation
SYNOPSIS
use Attribute::Lazy;
class Foo {
has $.foo will lazy { "zub" };
has $.booble will lazy { $_.bungle };
method bungle() {
'beep';
}
}
DESCRIPTION
This is based on an experimental trait that was briefly in the Rakudo core.
Attribute::Lazy provides a single trait
will lazy
that will allow
an attribute with a public accessor (that is one defined with the .
twigil,)
to be initialised the first time it is accessed by the result of the supplied
block. This might be useful if the value may not be used and may be expensive
to calculate (or various other reasons that haven't been thought of.)
The supplied block will have the object instance passed to it as an argument, which can be used to call other methods or public accessors on the object, you probably want to avoid calling anything that may depend on the value of the attribute for obvious reasons.