* Moose::Meta::Method::Delegation
- preserve aliasing for delegated methods (doy)
+ * Moose::Util::TypeConstraints
+ - allow arrayrefs for all forms of enum and duck_type, not just anonymous
+ (the non-arrayref forms may be removed in the future). (doy)
+
0.92 Tue, Sep 22, 2009
* Moose::Util::TypeConstraints
- added the match_on_type operator (Stevan)
from L<Class::MOP::Object>) are currently exempt from this check, since at the
moment we aren't very consistent about which metaclasses we immutabilize.
+=item C<enum> and C<duck_type> now take arrayrefs for all forms
+
+Previously, calling these functions with a list would take the first element of
+the list as the type constraint name, and use the remainder as the enum values
+or method names. This makes the interface inconsistent with the anon-type forms
+of these functions (which must take an arrayref), and a free-form list where
+the first value is sometimes special is hard to validate (and harder to give
+reasonable error messages for). These functions have been changed to take
+arrayrefs in all their forms - so, C<< enum 'My::Type' => [qw(foo bar)] >> is
+now the preferred way to create an enum type constraint. The old syntax still
+works for now, but it will hopefully be deprecated and removed in a future
+release.
+
=back
=head1 Version 0.89_01
@methods = @$type_name;
$type_name = undef;
}
+ if ( @methods == 1 && ref $methods[0] eq 'ARRAY' ) {
+ @methods = @{ $methods[0] };
+ }
register_type_constraint(
create_duck_type_constraint(
@values = @$type_name;
$type_name = undef;
}
+ if ( @values == 1 && ref $values[0] eq 'ARRAY' ) {
+ @values = @{ $values[0] };
+ }
( scalar @values >= 2 )
|| __PACKAGE__->_throw_error(
"You must have at least two values to enumerate through");
Creates a type constraint for either C<undef> or something of the
given type.
-=item B<duck_type ($name, @methods)>
+=item B<duck_type ($name, \@methods)>
This will create a subtype of Object and test to make sure the value
-C<can()> do the methods in C<@methods>.
+C<can()> do the methods in C<\@methods>.
This is intended as an easy way to accept non-Moose objects that
provide a certain interface. If you're using Moose classes, we
=item B<duck_type (\@methods)>
-If passed an ARRAY reference instead of the C<$name>, C<@methods>
-pair, this will create an unnamed duck type. This can be used in an
-attribute definition like so:
+If passed an ARRAY reference as the only parameter instead of the
+C<$name>, C<\@methods> pair, this will create an unnamed duck type.
+This can be used in an attribute definition like so:
has 'cache' => (
is => 'ro',
isa => duck_type( [qw( get_set )] ),
);
-=item B<enum ($name, @values)>
+=item B<enum ($name, \@values)>
This will create a basic subtype for a given set of strings.
The resulting constraint will be a subtype of C<Str> and
-will match any of the items in C<@values>. It is case sensitive.
+will match any of the items in C<\@values>. It is case sensitive.
See the L<SYNOPSIS> for a simple example.
B<NOTE:> This is not a true proper enum type, it is simply
=item B<enum (\@values)>
-If passed an ARRAY reference instead of the C<$name>, C<@values> pair,
-this will create an unnamed enum. This can then be used in an attribute
-definition like so:
+If passed an ARRAY reference as the only parameter instead of the
+C<$name>, C<\@values> pair, this will create an unnamed enum. This
+can then be used in an attribute definition like so:
has 'sort_order' => (
is => 'ro',
enum Letter => 'a'..'z', 'A'..'Z';
enum Language => 'Perl 5', 'Perl 6', 'PASM', 'PIR'; # any others? ;)
-enum Metacharacter => '*', '+', '?', '.', '|', '(', ')', '[', ']', '\\';
+enum Metacharacter => ['*', '+', '?', '.', '|', '(', ')', '[', ']', '\\'];
my @valid_letters = ('a'..'z', 'A'..'Z');
use strict;
use warnings;
-use Test::More tests => 4;
+use Test::More tests => 5;
use Test::Exception;
{
use Moose::Util::TypeConstraints;
duck_type 'DuckType' => qw(quack);
+ duck_type 'SwanType' => [qw(honk)];
has duck => (
isa => 'DuckType',
is => 'ro',
);
+ has other_swan => (
+ isa => 'SwanType',
+ is => 'ro',
+ );
+
}
# try giving it a duck
lives_ok { DucktypeTest->new( duck => RubberDuck->new ) }
'the RubberDuck lives okay';
+# try with the other constraint form
+lives_ok { DucktypeTest->new( other_swan => Swan->new ) } 'but a Swan can honk';