use Type::Standard -moose, qw(Int);
use Type::Standard -mouse, qw(Int);
use Type::Standard -moo, qw(Int);
####
use v5.16;
use Benchmark qw(cmpthese);
BEGIN {
$ENV{PERL_TYPE_TINY_XS} = 0;
}
package Example::Native {
use Moose;
has numbers => (
is => 'rw',
isa => 'ArrayRef[Str]',
);
__PACKAGE__->meta->make_immutable;
}
package Example::TT {
use Moose;
use Types::Standard qw(ArrayRef Str);
has numbers => (
is => 'rw',
isa => ArrayRef[Str],
);
__PACKAGE__->meta->make_immutable;
}
cmpthese -1, {
native => q{
my $obj = Example::Native->new(numbers => []);
$obj->numbers([0 .. $_]) for 1 .. 50;
},
tt => q{
my $obj = Example::TT->new(numbers => []);
$obj->numbers([0 .. $_]) for 1 .. 50;
},
};
__END__
Rate native tt
native 2511/s -- -45%
tt 4525/s 80% --
##
##
use Moose::Util::TypeConstraints;
coerce 'Str',
from 'ArrayRef', via { join "\n", @$_ };
##
##
package MyClass {
use Moose;
use Types::Standard qw(ArrayRef Str);
has data => (
is => 'ro',
isa => Str->plus_coercions(ArrayRef, sub { join "\n", @$_ }),
);
}
##
##
package MyClass {
use Moose;
use Types::Standard qw(Int);
has even_number => (
is => 'ro',
isa => Int->where(sub { $_ % 2 == 0 }),
);
}
##
##
package MyClass {
use Moose;
use Types::Standard qw(ArrayRef);
use MooseX::Types::Moose qw(Int);
has will_this_work => (
is => 'ro',
isa => ArrayRef[Int],
);
}
##
##
use v5.16;
package MyClass {
use Moose;
use Types::Standard qw(Object Int);
use Type::Params qw(compile);
my $EvenInt = Int->where(sub { $_ % 2 == 0 });
has even_number => (
is => 'ro',
isa => $EvenInt,
writer => '_set_even_number',
);
sub add_another_even {
state $check = compile(Object, $EvenInt);
my ($self, $n) = &$check;
$self->_set_even_number( $self->even_number + $n );
return $self;
}
}