Re: BUILD and TWEAK

2020-07-21 Thread Vadim Belman


Oh, and the thing I forgot to mention: contrary to BUILD and TWEAK, DESTROY is 
invoked by underlying backend VM. I.e. by MoarVM, or JVM, or JS because only 
the VM knows when exactly an object cease to exists.

Best regards,
Vadim Belman

> On Jul 21, 2020, at 4:53 AM, Richard Hainsworth  
> 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'.
> 
> 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?
> 
> 2) What actually calls these submethods?
> 
> The documentation says 'bless' indirectly calls submethod BUILD. But the 
> documentation seems to indicate that 'bless' is not needed to create an 
> object.
> 
> So what calls the submethods?
> 
> 3) Is it correct to say that TWEAK can access all the attributes of the 
> object?
> 
> 4) How to describe the way the TWEAK of a subclass accesses attributes in the 
> parent?
> 
> eg
> 
> class A { has $.thing }
> 
> class B is A { submethod TWEAK { #do something to $.thing in A } }
> 


Re: BUILD and TWEAK

2020-07-21 Thread Vadim Belman


> On Jul 21, 2020, at 4:53 AM, Richard Hainsworth  
> 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'.

s/BUILD/BEGIN/

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.

Best regards,
Vadim Belman