Front page | perl.perl6.users |
Postings from July 2020
Re: BUILD and TWEAK
From: Vadim Belman
July 21, 2020 20:45
Re: BUILD and TWEAK
Message ID: FFB93BDF-797B-4DC6-824F-132C6BBD3612@lflat.org
> On Jul 21, 2020, at 4:53 AM, Richard Hainsworth <email@example.com> wrote:
> Trying to update the documentation on submethod TWEAK - there is nothing.
> But the documentation should be accurate and not confusing. So some questions:
> In a loop or program, things like BUILD or LEAVE or FIRST are called 'phasers'.
BUILD is a constructor. Same applies to TWEAK. Both are invoked at construction time but have different semantics. BUILD must take care of attribute initialization. If not installed, the core installs one for you. TWEAK is invoked at the last stage of construction is is able to tweak object state, as the name suggests.
> By analogy, during the 'build' process of instantiating an object from a class, two 'submethod's can be called, viz.
> submethod BUILD and submethod TWEAK.
> 1) Should these submethods be considered 'phasers' to be consistent with other BUILD, LEAVE etc usages?
No. A phaser something responsible for a phase in code execution. BUILD, TWEAK, and DESTROY are responsible for object life cycle.
BTW, there was once a discussion about a common name for all of them. But I don't remember if there was any consensus reached. In either case, 'constructors' and 'destructors' terms are commonplace.
> 2) What actually calls these submethods?
With some level of simplification I can say that BUILD and TWEAK are invoked by method new. There is some indirection level though.
> The documentation says 'bless' indirectly calls submethod BUILD. But the documentation seems to indicate that 'bless' is not needed to create an object.
'bless' is kind of a simplified version of 'new'. You can find them in Rakudo src/core.c/Mu.pm.
> 3) Is it correct to say that TWEAK can access all the attributes of the object?
TWEAK can access all attributes of its class and public attributes of parent classes. BEGIN and TWEAK can't use `$.attribute` notation though, only `$!attribute` or `self.attribute`.
> 4) How to describe the way the TWEAK of a subclass accesses attributes in the parent?
It's done same way as accessing any parent method, as described above.