Re: BUILD and how to create a subclass object when creating a parent

[email protected] (Arjan Widlak - United Knowledge) Wed, 25 Apr 2018 22:56:24 +0200
Newsgroups perl.moose
Message-ID <[email protected]>
--------------A99FBBD6B45C61F48F3BE6E7
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit

And to also give a diagram, as you ask:

Asset::Container::KDU1        Response1 Response::Summary1
Del-> Asset::Container::KDU2 (6)    Response2 (5) Response::Summary2 (4)
Asset::Container::KDU3 (3)    Response3 (2) Response::Summary3 (1)

Above KDU2 is a child of KDU1, not in terms of classes, but in terms of 
lineage. Like a page and a subpage. A Response is the response of a user 
on a KDU. A Summary is a subclass of Response as described.

Now if I call delete on instance KDU2, I would like to see:

1 deletion of Summary3
2 deletion of Response3
3 deletion of KDU3
4 deletion of Summary2
5 deletion of Response2
6 deletion of KDU2

Kind regards,
Arjan.

On 25/04/18 19:54, Karen Etheridge wrote:
> I'm not sure you mean by create a subclass instance on creation of its 
> superclass. Are you intending to have the superclass object have an 
> attribute that holds an instance of the subclass? Perhaps can you show 
> a diagram of what you intend your data to look like after construction 
> is complete?
>
> On Mon, Apr 23, 2018 at 7:53 AM, Arjan Widlak - United Knowledge 
> <[email protected] <mailto:[email protected]>> wrote:
>
>     Hi,
>
>     I tried to use the BUILD method to create an instance of a
>     subclass on creating an instance of the parent. This does not work
>     and the documentation clearly explains why:
>
>     http://search.cpan.org/dist/Moose/lib/Moose/Manual/Construction.pod#BUILD_and_parent_classes
>     <http://search.cpan.org/dist/Moose/lib/Moose/Manual/Construction.pod#BUILD_and_parent_classes>
>
>     BUILD methods are called from parent to child and hence this
>     triggers a loop.
>
>     I am able to do this with the around method modifier, however I
>     need to override the behaviour in the subclass. This is also seems
>     error prone and bad practice.
>
>     But if this cannot be done this way, how should it be done? How
>     can I always create a subclass instance on creation of it's
>     superclass?
>
>     Kind regards,
>
>     Arjan
>
>


--------------A99FBBD6B45C61F48F3BE6E7
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 8bit

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    <div class="moz-cite-prefix">And to also give a diagram, as you ask:<br>
      <br>
      <font face="Courier New, Courier, monospace">     
        Asset::Container::KDU1        Response1       
        Response::Summary1<br>
        Del-&gt;  </font><font face="Courier New, Courier, monospace"><font
          face="Courier New, Courier, monospace">Asset::Container::</font>KDU2
        (6)    Response2 (5)    </font><font face="Courier New,
        Courier, monospace"><font face="Courier New, Courier, monospace">Response::</font>Summary2
        (4)<br>
                </font><font face="Courier New, Courier, monospace"><font
          face="Courier New, Courier, monospace">Asset::Container::</font>KDU3
        (3)    Response3 (2)    </font><font face="Courier New,
        Courier, monospace"><font face="Courier New, Courier, monospace">Response::</font>Summary3
        (1)<br>
        <br>
      </font>Above KDU2 is a child of KDU1, not in terms of classes, but
      in terms of lineage. Like a page and a subpage. A Response is the
      response of a user on a KDU. A Summary is a subclass of Response
      as described. <br>
      <br>
      Now if I call delete on instance KDU2, I would like to see:<br>
      <br>
      1 deletion of Summary3<br>
      2 deletion of Response3<br>
      3 deletion of KDU3<br>
      4 deletion of Summary2<br>
      5 deletion of Response2<br>
      6 deletion of KDU2<br>
      <br>
      Kind regards,<br>
      Arjan.<br>
      <br>
      On 25/04/18 19:54, Karen Etheridge wrote:<br>
    </div>
    <blockquote type="cite"
cite="mid:CAPJsHfAGoPRiW+aLSs3Hn8u534SWz0Uwzvb+smi0Cze+xXhtdQ@mail.gmail.com">
      <div dir="ltr">
        <div class="gmail_default" style="font-size:small">I'm not sure
          you mean by create a subclass instance on creation of its
          superclass. Are you intending to have the superclass object
          have an attribute that holds an instance of the subclass? 
          Perhaps can you show a diagram of what you intend your data to
          look like after construction is complete?<br>
        </div>
      </div>
      <div class="gmail_extra"><br>
        <div class="gmail_quote">On Mon, Apr 23, 2018 at 7:53 AM, Arjan
          Widlak - United Knowledge <span dir="ltr">&lt;<a
              href="mailto:[email protected]" target="_blank"
              moz-do-not-send="true">[email protected]</a>&gt;</span>
          wrote:<br>
          <blockquote class="gmail_quote" style="margin:0 0 0
            .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
            <br>
            I tried to use the BUILD method to create an instance of a
            subclass on creating an instance of the parent. This does
            not work and the documentation clearly explains why:<br>
            <br>
            <a
href="http://search.cpan.org/dist/Moose/lib/Moose/Manual/Construction.pod#BUILD_and_parent_classes"
              rel="noreferrer" target="_blank" moz-do-not-send="true">http://search.cpan.org/dist/Mo<wbr>ose/lib/Moose/Manual/Construct<wbr>ion.pod#BUILD_and_parent_<wbr>classes</a><br>
            <br>
            BUILD methods are called from parent to child and hence this
            triggers a loop.<br>
            <br>
            I am able to do this with the around method modifier,
            however I need to override the behaviour in the subclass.
            This is also seems error prone and bad practice.<br>
            <br>
            But if this cannot be done this way, how should it be done?
            How can I always create a subclass instance on creation of
            it's superclass?<br>
            <br>
            Kind regards,<br>
            <br>
            Arjan<br>
          </blockquote>
        </div>
        <br>
      </div>
    </blockquote>
    <p><br>
    </p>
  </body>
</html>

--------------A99FBBD6B45C61F48F3BE6E7--