Moo, lazy attributes and the default / coerce call

In my class on the basis of Mu, there are lazy, and lazy attributes that have both default

, and coerce

subs. If I don't initialize the attributes, I find that for a normal attribute default

, both coerce

subs are called, but for a lazy attribute, only is called default

. This seems inconsistent. Here's some sample code:

package Foo;
use Moo;

has nrml => ( is => 'ro',
              default => sub { print "nrml default\n" },
              coerce  => sub { print "nrml coerce\n" } 
            );

has lazy => ( is => 'ro',
              lazy => 1,
              default => sub { print "lazy default\n" },
              coerce  => sub { print "lazy coerce\n" }
            );



my $q = Foo->new( );

$q->lazy;

      

Output:

nrml default
nrml coerce
lazy default

      

I expect to coerce

be fired if I provided a value in the constructor. Moreover, I expect the same sequence of execution (either default

, or default

and coerce

) from lazy and normal attributes.

So, are my expectations being met, is this a mistake, or what? Thank!

+3


source to share


2 answers


Current status: fixed sent to 009014

One of these two is a bug.

In fact, thinking about it, one could argue anyway about whether coercions should be fired by default, but since Moose does this, and since coercions are structural (as opposed to typechecking, which is often used to assert a thing and should always pass unless there is an error), I think it does.



... actually the problem is that the :: Generate :: Accessor method when starting _use_default always wraps it in _generate_simple_set, when it's _generate_set, which provides the isa + coerce + trigger transaction, and I'm pretty sure Moose triggers all three when applied by default, so we also need.

This is not exactly a trivial fix, because I did not parameterize _generate_set to accept a value indicating how to generate a value to set. I will try and disassemble it tomorrow as I plan on cutting out the issue.

If you want to support Moo with developers, please contact bugs-Moo@rt.cpan.org or join # web-simple at irc.perl.org - it's just luck someone on the IRC channel saw this question and asked about this :)

+5


source


This would be a mistake for me. The value from is default

expected to be of the desired type or not. Having and meeting expectations only half the time doesn't make sense.



+3


source







All Articles