in reply to Re: Moose and BUILD
in thread Moose and BUILD

The type is specified using isa, and you need lazy => 1 to make sure the default isn't calculated too soon.

use Moose; has 'x' => ( is => 'rw' isa => 'Num', ); has 'y' => ( is => 'rw', isa => 'Num', lazy => 1, default => sub { sqrt($_[0])->x) }, );

Replies are listed 'Best First'.
Re^3: Moose and BUILD
by tj_thompson (Monk) on Jun 15, 2011 at 21:04 UTC

    Good catch on the is, just a brain typo from rapidly throwing together running example code. It didn't matter in this case as I wasn't trying to use bad values in my test script.

    I will look into the lazy flag on the attribute and get a better idea of what it does.

      Always use lazy when the initialiser relies on another field of $self.

      It cause the initialiser to be called when the field is read instead of being called on construction.