Closed robrwo closed 2 years ago
It's possible that discussion in #27 relates to this.
I'll give a more concrete example. I maintain some Catalyst and Mojo web applications. At $work we have various helper classes for the business logic. It's often useful to say in the application
my $x = Foo->new( ctx => $c );
where $c
is the Catalyst context. It's a monstrously huge object that Foo
only needs to initialize attributes from, but otherwise can simply be thrown away once the object is built.
After considerable discussion, for the MVP, there is no BUILDARGS
or equivalent. We use an alternate constructor.
common method new_from_ctx (%arg_for) {
my $c = $arg_for{ctx} or croak("You must pass ctx to new_from_ctx");
my %args = # extract args from $c
return $class->new(%args);
}
We will revisit this after the MVP unless we there is a compelling reason it must exist for the MVP.
Closing this to ensure tickets are just for the MVP. We will revisit later.
Moo(se) have
BUILDARGS
which I have found to be very useful. A common use case:using a
BUILDARGS
method allows us to add constructor-only slots:or even
(Requiring users to explicitly declare every slot ticks a box for making come self-explanatory, but one of the things that make Perl a powerful language is the ability to add shortcuts to "Do what I mean".)
In the above use case, would could work around this by having a
foo
slot in the Bar object and the other slots lazily default to the appropriate values from it, but what if the Bar object just doesn't need it (especially if thefoo
takes up a lot of memory)?Being able to rewrite or adjust constructor arguments is a useful feature, especially as code evolves and a developer wants to gracefully handle older arguments.