From: Jesse Luehrs Date: Wed, 5 Oct 2011 16:46:37 +0000 (-0500) Subject: deprecate non-arrayref enum and duck_type X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMoose.git;a=commitdiff_plain;h=rfc%2Fdeprecate-nonarrayref-enum deprecate non-arrayref enum and duck_type --- diff --git a/Changes b/Changes index fb1ff41..784554b 100644 --- a/Changes +++ b/Changes @@ -3,6 +3,11 @@ for, noteworthy changes. {{$NEXT}} + [DEPRECATIONS] + + * The non-arrayref forms of enum and duck_type have been deprecated. See + Moose::Manual::Delta for details. (doy) + [ENHANCEMENTS] * The super() subroutine now carps if you pass it arguments. These arguments diff --git a/lib/Moose/Deprecated.pm b/lib/Moose/Deprecated.pm index 312604f..b371496 100644 --- a/lib/Moose/Deprecated.pm +++ b/lib/Moose/Deprecated.pm @@ -4,6 +4,8 @@ use strict; use warnings; use Package::DeprecationManager 0.07 -deprecations => { + 'non-arrayref form of enum' => '2.0700', + 'non-arrayref form of duck_type' => '2.0700', 'optimized type constraint sub ref' => '2.0000', 'default is for Native Trait' => '1.14', 'default default for Native Trait' => '1.14', diff --git a/lib/Moose/Manual/Delta.pod b/lib/Moose/Manual/Delta.pod index a804cd2..1720719 100644 --- a/lib/Moose/Manual/Delta.pod +++ b/lib/Moose/Manual/Delta.pod @@ -19,6 +19,35 @@ feature. If you encounter a problem and have a solution but don't see it documented here, or think we missed an important feature, please send us a patch. +=head1 2.0700 + +=over 4 + +=item The non-arrayref forms of C and C have been deprecated + +Originally, C could be called like this: + + enum('MyType' => qw(foo bar baz)) + +This was confusing, however (since it was different from the syntax for +anonymous enum types), and it makes error checking more difficult (since you +can't tell just by looking whether C was intended to +be a type named C with elements of C and C, or if this was +actually a mistake where someone got the syntax for an anonymous enum type +wrong. This all also applies to C. + +Calling C and C with a list of arguments as described above +has been undocumented since version 0.93, and is now deprecated. You should +replace + + enum MyType => qw(foo bar baz); + +in your code with + + enum MyType => [qw(foo bar baz)]; + +=back + =head1 2.0600 =over 4 diff --git a/lib/Moose/Util/TypeConstraints.pm b/lib/Moose/Util/TypeConstraints.pm index b44a0eb..1418586 100644 --- a/lib/Moose/Util/TypeConstraints.pm +++ b/lib/Moose/Util/TypeConstraints.pm @@ -5,6 +5,7 @@ use Carp (); use List::MoreUtils qw( all any ); use Scalar::Util qw( blessed reftype ); use Moose::Exporter; +use Moose::Deprecated; ## -------------------------------------------------------- # Prototyped subs must be predeclared because we have a @@ -377,12 +378,19 @@ sub maybe_type { sub duck_type { my ( $type_name, @methods ) = @_; if ( ref $type_name eq 'ARRAY' && !@methods ) { - @methods = @$type_name; + @methods = ($type_name); $type_name = undef; } if ( @methods == 1 && ref $methods[0] eq 'ARRAY' ) { @methods = @{ $methods[0] }; } + else { + Moose::Deprecated::deprecated( + feature => 'non-arrayref form of duck_type', + message => "Passing a list of values to duck_type is deprecated. " + . "The method names should be wrapped in an arrayref.", + ); + } register_type_constraint( create_duck_type_constraint( @@ -430,12 +438,19 @@ sub enum { @values == 0 || __PACKAGE__->_throw_error("enum called with an array reference and additional arguments. Did you mean to parenthesize the enum call's parameters?"); - @values = @$type_name; + @values = ($type_name); $type_name = undef; } if ( @values == 1 && ref $values[0] eq 'ARRAY' ) { @values = @{ $values[0] }; } + else { + Moose::Deprecated::deprecated( + feature => 'non-arrayref form of enum', + message => "Passing a list of values to enum is deprecated. " + . "Enum values should be wrapped in an arrayref.", + ); + } register_type_constraint( create_enum_type_constraint( diff --git a/t/examples/example_Moose_POOP.t b/t/examples/example_Moose_POOP.t index 8d6e28a..19c86e0 100644 --- a/t/examples/example_Moose_POOP.t +++ b/t/examples/example_Moose_POOP.t @@ -186,7 +186,7 @@ BEGIN { => as 'Str' => where { DateTime::Format::MySQL->parse_datetime($_) }; - enum 'Status' => qw(draft posted pending archive); + enum 'Status' => [qw(draft posted pending archive)]; has 'headline' => (is => 'rw', isa => 'Headline'); has 'summary' => (is => 'rw', isa => 'Summary'); diff --git a/t/type_constraints/duck_type_handles.t b/t/type_constraints/duck_type_handles.t index 67971b7..55a0db7 100644 --- a/t/type_constraints/duck_type_handles.t +++ b/t/type_constraints/duck_type_handles.t @@ -28,7 +28,7 @@ my @phonograph; use Moose; use Moose::Util::TypeConstraints; - my $ducktype = duck_type 'DuckType' => qw(walk quack); + my $ducktype = duck_type 'DuckType' => [qw(walk quack)]; has duck => ( isa => $ducktype, diff --git a/t/type_constraints/duck_types.t b/t/type_constraints/duck_types.t index f0763c8..1aef5b5 100644 --- a/t/type_constraints/duck_types.t +++ b/t/type_constraints/duck_types.t @@ -38,7 +38,7 @@ use Test::Fatal; use Moose; use Moose::Util::TypeConstraints; - duck_type 'DuckType' => qw(quack); + duck_type 'DuckType' => [qw(quack)]; duck_type 'SwanType' => [qw(honk)]; has duck => ( diff --git a/t/type_constraints/enum.t b/t/type_constraints/enum.t index 7349c48..2e77d38 100644 --- a/t/type_constraints/enum.t +++ b/t/type_constraints/enum.t @@ -10,8 +10,8 @@ use Scalar::Util (); use Moose::Util::TypeConstraints; -enum Letter => 'a'..'z', 'A'..'Z'; -enum Language => 'Perl 5', 'Perl 6', 'PASM', 'PIR'; # any others? ;) +enum Letter => ['a'..'z', 'A'..'Z']; +enum Language => ['Perl 5', 'Perl 6', 'PASM', 'PIR']; # any others? ;) enum Metacharacter => ['*', '+', '?', '.', '|', '(', ')', '[', ']', '\\']; my @valid_letters = ('a'..'z', 'A'..'Z'); diff --git a/t/type_constraints/match_type_operator.t b/t/type_constraints/match_type_operator.t index 4304487..ca5d45d 100644 --- a/t/type_constraints/match_type_operator.t +++ b/t/type_constraints/match_type_operator.t @@ -69,8 +69,8 @@ is( break_it_down(), 'undef', '... got the right value'); # checking against enum types -enum RGB => qw[ red green blue ]; -enum CMYK => qw[ cyan magenta yellow black ]; +enum RGB => [qw[ red green blue ]]; +enum CMYK => [qw[ cyan magenta yellow black ]]; sub is_acceptable_color { match_on_type shift, diff --git a/t/type_constraints/util_std_type_constraints.t b/t/type_constraints/util_std_type_constraints.t index 31e22b4..a75c919 100644 --- a/t/type_constraints/util_std_type_constraints.t +++ b/t/type_constraints/util_std_type_constraints.t @@ -843,7 +843,7 @@ foreach my $type_name (qw(Str Num Int ClassName RoleName)) { my @methods = qw( quack flap ); - duck_type 'Duck' => @methods; + duck_type 'Duck' => \@methods; test_constraint( 'Duck', { @@ -885,7 +885,7 @@ foreach my $type_name (qw(Str Num Int ClassName RoleName)) { my @allowed = qw( bar baz quux ); - enum 'Enumerated' => @allowed; + enum 'Enumerated' => \@allowed; test_constraint( 'Enumerated', { @@ -1087,8 +1087,8 @@ foreach my $type_name (qw(Str Num Int ClassName RoleName)) { - enum 'Enum1' => 'a', 'b'; - enum 'Enum2' => 'x', 'y'; + enum 'Enum1' => ['a', 'b']; + enum 'Enum2' => ['x', 'y']; subtype 'EnumUnion', as 'Enum1 | Enum2';