[Melbourne-pm] Moose/Moo type constraints
Toby Corkindale
toby.corkindale at strategicdata.com.au
Fri Jun 21 01:06:23 PDT 2013
Hi,
I just wondered if there's a Moose constraint that checks the expected
arguments of a coderef?
ie. In the following program, it will die if you call $area_sub->() with
anything other than a single, numeric, parameter. I just wondered if you
can introspect that during the Moose object constructor, since the
function has been defined using Moose types too and thus has that info
available somewhere..
I have a suspicion the answer is "No" but I'm curious in case I missed
this somewhere.
use Method::Signatures;
# This version should succeed:
my $item = Object->new(
x => 12, y => 34,
radius => 6.99,
area_calculator => func (Num r) { 3.14 * r * r }
);
# This call should die with invalid argument exception:
my $item = Object->new(
x => 12, y => 34,
radius => 6.99,
area_calculator => func (Int age, Str name) { length(name) * age }
);
package Object;
use Moose;
use Method::Signatures;
has [qw(x y radius)] => (
is => 'ro',
isa => 'Num',
required => 1,
);
has area_calculator => (
is => 'ro',
required => 1,
isa => subtype( 'CodeRef' => where { ... } ),
);
method area { $self->area_calculator->($self->radius) }
1;
More information about the Melbourne-pm
mailing list