Subject: | A more convenient way to subvert strictness |
I'm stuck.
On one hand, I'd like to use MooseX::StrictConstructor to catch typos in
hash keys, etc. On the other hand, I have several composite objects
that blindly pass their constructor arguments to other constructors.
For example:
package Foo;
has x => ( is => 'ro', isa => 'Str');
has bar => ( is => 'ro', isa => 'Bar');
has baz => ( is => 'ro', isa => 'Baz');
BUILDARGS {
my ($class, %args) = @_;
$args{bar} = Bar->new(%args);
$args{baz} = Baz->new(%args);
return \%args;
}
package main;
my $foo = Foo->new( x => $arg_for_foo,
y => $arg_for_bar,
z => $arg_for_baz);
Of course, anyone constructing a Foo doesn't necessarily know or care
that y and z land in a Bar and a Baz. (I believe this pattern is
reasonable and quite perlish, but feel free to enlighten me).
In the documentation, you recommend deleting the "extra" attributes from
the constructor arguments. But this seems really tedious to me, and it
starts getting ugly if you want to pass an attribute to more than one place.
I think what I would like is a way to just declare the "extra"
attributes that could be expected. And then I'd only get an exception
of I pass a constructor argument that is neither declared as an
attribute, nor as an "extra". Perhaps MooseX::StrictConstructor could
be parameterized like so:
package Foo;
use Moose;
use MooseX::StrictConstructor (allow => [ qw(bar baz) ]);
has qux => (...)
#-----
Foo->new(qux => 1, bar => 2, baz => 3); # This would be ok
Foo->new(qux => 1, ber => 2); # This would fail
What do you think?
-Jeff