Using Types::Standard, MooX::HandlesVia, and Moo–
#!/usr/bin/env perl { package FishTaco::Topping; use Moo; } { package FishTaco; use Moo; use MooX::HandlesVia; use Types::Standard qw( ArrayRef InstanceOf ); has _toppings => is => "rw", isa => ArrayRef[ InstanceOf['FishTaco::Topping'] ], default => sub { [] }, handles_via => "Array", handles => { add_topping => "push", toppings => "elements", }; } use strictures; use Path::Tiny; use Scalar::Util "blessed"; my $ft = FishTaco->new; $ft->add_topping( FishTaco::Topping->new ); # Sure. $ft->add_topping( path("/") ); # Wrong object type. $ft->add_topping( "OHAI" ); # Not even an object. print blessed($_) || "[n/a]", $/ for $ft->toppings; __END__ FishTaco::Topping Path::Tiny [n/a]
–but I expect the latter two calls to add_topping to throw an error since they “break” the isa of ArrayRef[ InstanceOf['FishTaco::Topping'] ]. I’m sure I’m doing something wrong or misunderstanding, but what? :P
(Update: title was badly worded.)
In reply to Trouble with, or misunderstanding of, Types::Standard ArrayRef[elements] enforcement in Moo by Your Mother
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |