Skip Menu |

This queue is for tickets about the MooseX-StrictConstructor CPAN distribution.

Report information
The Basics
Id: 70725
Status: resolved
Priority: 0/
Queue: MooseX-StrictConstructor

People
Owner: Nobody in particular
Requestors: thaljef [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: 0.16
Fixed in: (no value)



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
Subject: Re: [rt.cpan.org #70725] A more convenient way to subvert strictness
Date: Sun, 4 Sep 2011 18:07:22 -0500 (CDT)
To: Jeffrey Ryan Thalhammer via RT <bug-MooseX-StrictConstructor [...] rt.cpan.org>
From: Dave Rolsky <autarch [...] urth.org>
On Sun, 4 Sep 2011, Jeffrey Ryan Thalhammer via RT wrote: Show quoted text
> 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?
I think it might make more sense to write a MooseX::ContainedsObject role that lets you declare that you contain other objects. That role could override BUILDARGS to massage the arguments properly. -dave /*============================================================ http://VegGuide.org http://blog.urth.org Your guide to all that's veg House Absolute(ly Pointless) ============================================================*/
Subject: Re: [rt.cpan.org #70725] A more convenient way to subvert strictness
Date: Sun, 4 Sep 2011 19:22:56 -0700
To: bug-MooseX-StrictConstructor [...] rt.cpan.org
From: Jeffrey Thalhammer <jeff [...] imaginative-software.com>
On Sep 4, 2011, at 4:07 PM, autarch@urth.org via RT wrote: Show quoted text
> I think it might make more sense to write a MooseX::ContainedsObject role > that lets you declare that you contain other objects. That role could > override BUILDARGS to massage the arguments properly.
At first, I though the basic has-a relationship seemed so trivial that I would hardly need a Moose extension. But after a while, I realized that mine was a particular kind of has-a relationship. Basically, my constructor is a proxy for the constructors of the objects it contains. And that sounds like an opportunity for a MooseX. .oO( Now if only I could magically get the POD from the composed classes into the proxy class ). Thanks for the idea! -Jeff
Subject: Re: [rt.cpan.org #70725] A more convenient way to subvert strictness
Date: Sun, 4 Sep 2011 21:48:35 -0500 (CDT)
To: Jeffrey Thalhammer via RT <bug-MooseX-StrictConstructor [...] rt.cpan.org>
From: Dave Rolsky <autarch [...] urth.org>
On Sun, 4 Sep 2011, Jeffrey Thalhammer via RT wrote: Show quoted text
> Queue: MooseX-StrictConstructor > Ticket <URL: https://rt.cpan.org/Ticket/Display.html?id=70725 > > > > On Sep 4, 2011, at 4:07 PM, autarch@urth.org via RT wrote: >
>> I think it might make more sense to write a MooseX::ContainedsObject role >> that lets you declare that you contain other objects. That role could >> override BUILDARGS to massage the arguments properly.
> > At first, I though the basic has-a relationship seemed so trivial that I would hardly need a Moose extension. > > But after a while, I realized that mine was a particular kind of has-a relationship. Basically, my constructor is a proxy for the constructors of the objects it contains. And that sounds like an opportunity for a MooseX. .oO( Now if only I could magically get the POD from the composed classes into the proxy class ). > > Thanks for the idea!
There's actually precedent for this in Perl land. There's a module called Class::Container on CPAN that I contributed a bit too. We wrote it for Mason way back when. It's a lot of hairy code that becomes infinitely simpler with Moose. -dave /*============================================================ http://VegGuide.org http://blog.urth.org Your guide to all that's veg House Absolute(ly Pointless) ============================================================*/