|go ahead... be a heretic|
Caching a default instance to avoid initializing it every time (with Moo)by Dallaylaen (Hermit)
|on Feb 08, 2018 at 14:19 UTC||Need Help??|
Dallaylaen has asked for the
wisdom of the Perl Monks concerning the following question:
Hello dear esteemed monks,
I have a class that is read-only. It does have parameters, but for the most cases a default instance would fit. So I'd like to avoid unneeded initialization and just return $ONE_AND_TRUE_INSTANCE if such exists and no parameters are given to new().
Here's how I would've done this outside Moo/Moose:
Can the same be achieved with Moo? How can my example code be improved? Maybe there's a proper architectural solution and/or pattern for this?
UPDATE: actually I can do this with Moo, so the question remains - what can be fixed in architecture to avoid the need for "defaulton":