as_is => [qw(
as where message optimize_as
from via
- type subtype coerce class_type role_type enum
+
+ type subtype class_type role_type duck_type
+ enum
+ coerce
+
find_type_constraint
)],
);
);
}
+sub duck_type {
+ my($name, @methods);
+
+ if(!(@_ == 1 && ref($_[0]) eq 'ARRAY')){
+ $name = shift;
+ }
+
+ @methods = (@_ == 1 && ref($_[0]) eq 'ARRAY') ? @{$_[0]} : @_;
+
+ return _create_type 'type', $name => (
+ optimized_as => Mouse::Util::generate_can_predicate_for(\@methods),
+
+ type => 'DuckType',
+ );
+}
+
sub typecast_constraints { # DEPRECATED
my($class, $pkg, $type, $value) = @_;
Carp::croak("wrong arguments count") unless @_ == 4;
sub enum {
my($name, %valid);
- # enum ['small', 'medium', 'large']
- if (ref($_[0]) eq 'ARRAY') {
- %valid = map{ $_ => undef } @{ $_[0] };
- $name = sprintf '(%s)', join '|', sort @{$_[0]};
- }
- # enum size => 'small', 'medium', 'large'
- else{
- $name = shift;
- %valid = map{ $_ => undef } @_;
+ if(!(@_ == 1 && ref($_[0]) eq 'ARRAY')){
+ $name = shift;
}
+
+ %valid = map{ $_ => undef } (@_ == 1 && ref($_[0]) eq 'ARRAY' ? @{$_[0]} : @_);
+
return _create_type 'type', $name => (
optimized_as => sub{ defined($_[0]) && !ref($_[0]) && exists $valid{$_[0]} },