Hi Karen,
Thank you for your reply. Sorry if it's not clear, let me try again with
a more concrete example.
I have a class called Asset, with subclasses like so:
Asset
Asset::Container
Asset::Container::KDU
Assets are objects to be displayed, Containers are Assets, but can
contain other Assets and Knowledge Delivery Units are a special kind of
containers.
I have a similar group of classes with subclasses like so:
Response
Response::Container
A Response is some response on an Asset by a user. The
Response::Container subclass sees if all Assets in a Container have
received a Response. It's like a summary of it's parent class.
Now the problem. If I create a Response, I might like to create a
Response::Container instance as well. If the Response I am about to
create is the last Response needed for all Assets in the Container to
have received a Response, than I would like to create a
Response::Container subclass instance.
So creation of an instance of a subclass - Response::Container - is only
dependent on the creation of the parent class - Response. Therefore it
seems like a good design choice to couple this to the create method of
Response. (Where the create method does new, update the database, and
new_by_id, to re-instantiate from the database.)
How does the Moose framework envision such a case? I expected BUILD to
be the answer. It causes a deep recursion, so it's definitely not, as is
well documented. Around also causes a deep recursion by default if one
does this, that can be avoided though by creating an empty subroutine in
the subclass. Quite error prone, so not the obvious choice.
Since many of the ideas in Moose - Roles, Method modifiers, etc. - are
design ideas, I thought there is probably a view on cases like this.
However I cannot find the obvious way to to couple the creation of a
subclass to the creation of a parent class in Moose. How would one be
expected to do this in Moose?
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
<ar...@unitedknowledge.nl <mailto:ar...@unitedknowledge.nl>> 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