On Thu, Sep 8, 2016 at 6:41 AM, Kamil Kułaga <teodoz...@gmail.com> wrote:

> In perl6 default way is to not write new, BUILD or BUILDALL and also
> not to write accessors. When you create object you can provide
> attributes to initialize, default accessors are generated if field is
> declared with $. sigil.
>

Right, there's a default "new" method that takes care of attribute
initialization, and calling any ancestor class initializers.

To have your initializer pre-process the attributes before building the
object, or call other code on creation- declare a "submethod BUILD" - that
will get the arguments that "new" had and let you modify them, run code as
needed. Effectively it's intercepting the object creation process just
before the object is initialized.

That took me a long time to figure out when I was trying some Perl6 around
2013, and I didn't really get it until reading through the perl5 Moose
cookbook- and Moose is different enough from Perl6 that, while it helped me
get the big picture of Perl6 objects, it also took me a little more time to
unlearn some big details to get back into Perl6 (like how there is no
BUILDARGS). This paragraph is for anyone who is working on the docs- Perl6
could use an object tutorial and cookbook like Moose's!
https://docs.perl6.org/language/objects is actually pretty good, it just
wasn't around when I started- and Moose does have a lot of docs built up
from answering these kinds of questions.

 As for "new", I re-read https://docs.perl6.org/language/objects and see "if
you want a constructor that accepts positional arguments, you must write
your own new method: [example] ... However this is considered poor
practice, because it makes correct initialization of objects from
subclasses harder."

-y

Reply via email to