这有效:
use Moops;
class Foo :ro {
use Types::Common::Numeric qw(PositiveOrZeroInt);
has from => required => true, isa => PositiveOrZeroInt;
has to => required => true, isa => PositiveOrZeroInt, trigger => method($to) {
die 'must be from ≤ to' unless $self->from <= $to
};
}
Foo->new(from => 0, to => 1); # ok
Foo->new(from => 1, to => 0); # "must be from ≤ to at …"
我希望能够以某种方式使约束成为类型的一部分。
use Moops;
class Bar :ro {
use Types::Common::Numeric qw(PositiveOrZeroInt);
has from => required => true, isa => PositiveOrZeroInt;
has to => required => true, isa => PositiveOrZeroInt->where(sub {
$self->from <= $_
});
}
Bar->new(from => 0, to => 1);
# Global symbol "$self" requires explicit package name
# (did you forget to declare "my $self"?)
我检查了where
sub只接收一个参数。