From: Jesse Luehrs Date: Thu, 5 May 2011 15:45:13 +0000 (-0500) Subject: clean up type constraint declaration syntax in the docs a bit X-Git-Tag: 2.0003~62 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=4278a980d0f11690024ec542dc35a47c65b1f3bf;p=gitmo%2FMoose.git clean up type constraint declaration syntax in the docs a bit --- diff --git a/lib/Moose/Manual/Types.pod b/lib/Moose/Manual/Types.pod index c314168..cd0dde6 100644 --- a/lib/Moose/Manual/Types.pod +++ b/lib/Moose/Manual/Types.pod @@ -172,10 +172,10 @@ higher), which Moose will use if possible to display the invalid value. Here's a simple (and useful) subtype example: - subtype 'PositiveInt' - => as 'Int' - => where { $_ > 0 } - => message { "The number you provided, $_, was not a positive number" } + subtype 'PositiveInt', + as 'Int', + where { $_ > 0 }, + message { "The number you provided, $_, was not a positive number" }; Note that the sugar functions for working with types are all exported by L. @@ -215,12 +215,12 @@ robust than the string type parsing for complex cases. A coercion lets you tell Moose to automatically convert one type to another. - subtype 'ArrayRefOfInts' - => as 'ArrayRef[Int]'; + subtype 'ArrayRefOfInts', + as 'ArrayRef[Int]'; - coerce 'ArrayRefOfInts' - => from 'Int' - => via { [ $_ ] }; + coerce 'ArrayRefOfInts', + from 'Int', + via { [ $_ ] }; You'll note that we created a subtype rather than coercing C directly. It's a bad idea to add coercions to the raw built in @@ -251,13 +251,13 @@ object will have C<[ 42 ]> as its C attribute. Deep coercion is the coercion of type parameters for parameterized types. Let's take these types as an example: - subtype 'HexNum' - => as 'Str' - => where { /[a-f0-9]/i }; + subtype 'HexNum', + as 'Str', + where { /[a-f0-9]/i }; - coerce 'Int' - => from 'HexNum' - => via { hex $_ }; + coerce 'Int', + from 'HexNum', + via { hex $_ }; has 'sizes' => ( is => 'ro', @@ -271,15 +271,15 @@ attribute, Moose will not do any coercion. However, you can define a set of subtypes to enable coercion between two parameterized types. - subtype 'ArrayRefOfHexNums' - => as 'ArrayRef[HexNum]'; + subtype 'ArrayRefOfHexNums', + as 'ArrayRef[HexNum]'; - subtype 'ArrayRefOfInts' - => as 'ArrayRef[Int]'; + subtype 'ArrayRefOfInts', + as 'ArrayRef[Int]'; - coerce 'ArrayRefOfInts' - => from 'ArrayRefOfHexNums' - => via { [ map { hex } @{$_} ] }; + coerce 'ArrayRefOfInts', + from 'ArrayRefOfHexNums', + via { [ map { hex } @{$_} ] }; Foo->new( sizes => [ 'a1', 'ff', '22' ] ); @@ -289,9 +289,9 @@ Moose does not attempt to chain coercions, so it will not coerce a single hex number. To do that, we need to define a separate coercion: - coerce 'ArrayRefOfInts' - => from 'HexNum' - => via { [ hex $_ ] }; + coerce 'ArrayRefOfInts', + from 'HexNum', + via { [ hex $_ ] }; Yes, this can all get verbose, but coercion is tricky magic, and we think it's best to make it explicit. @@ -366,7 +366,7 @@ details. One helper worth noting is C, which allows you to create a subtype of C that only allows the specified values: - enum 'RGB' => qw( red green blue ); + enum 'RGB', [qw( red green blue )]; This creates a type named C. diff --git a/lib/Moose/Util/TypeConstraints.pm b/lib/Moose/Util/TypeConstraints.pm index 74d73ff..36146a8 100644 --- a/lib/Moose/Util/TypeConstraints.pm +++ b/lib/Moose/Util/TypeConstraints.pm @@ -883,20 +883,20 @@ __END__ use Moose::Util::TypeConstraints; - subtype 'Natural' - => as 'Int' - => where { $_ > 0 }; + subtype 'Natural', + as 'Int', + where { $_ > 0 }; - subtype 'NaturalLessThanTen' - => as 'Natural' - => where { $_ < 10 } - => message { "This number ($_) is not less than ten!" }; + subtype 'NaturalLessThanTen', + as 'Natural', + where { $_ < 10 }, + message { "This number ($_) is not less than ten!" }; - coerce 'Num' - => from 'Str' - => via { 0+$_ }; + coerce 'Num', + from 'Str', + via { 0+$_ }; - enum 'RGBColors' => qw(red green blue); + enum 'RGBColors', [qw(red green blue)]; no Moose::Util::TypeConstraints; @@ -938,7 +938,7 @@ this, as well as future proof your subtypes from classes which have yet to have been created, is to quote the type name: use DateTime; - subtype 'DateTime' => as 'Object' => where { $_->isa('DateTime') }; + subtype 'DateTime', as 'Object', where { $_->isa('DateTime') }; =head2 Default Type Constraints @@ -1017,12 +1017,12 @@ For instance, this is how you could use it with L to declare a completely new type. type 'HashOfArrayOfObjects', - { - where => IsHashRef( - -keys => HasLength, - -values => IsArrayRef(IsObject) - ) - }; + where { + IsHashRef( + -keys => HasLength, + -values => IsArrayRef(IsObject) + )->(@_); + }; For more examples see the F test file. @@ -1030,8 +1030,8 @@ file. Here is an example of using L and it's non-test related C function. - type 'ArrayOfHashOfBarsAndRandomNumbers' - => where { + type 'ArrayOfHashOfBarsAndRandomNumbers', + where { eq_deeply($_, array_each(subhashof({ bar => isa('Bar'), @@ -1070,7 +1070,7 @@ See the L for an example of how to use these. =over 4 -=item B<< subtype 'Name' => as 'Parent' => where { } ... >> +=item B<< subtype 'Name', as 'Parent', where { } ... >> This creates a named subtype. @@ -1086,7 +1086,7 @@ name and a hashref of parameters: The valid hashref keys are C (the parent), C, C, and C. -=item B<< subtype as 'Parent' => where { } ... >> +=item B<< subtype as 'Parent', where { } ... >> This creates an unnamed subtype and will return the type constraint meta-object, which will be an instance of @@ -1188,7 +1188,7 @@ B You should only use this if you know what you are doing. All the built in types use this, so your subtypes (assuming they are shallow) will not likely need to use this. -=item B<< type 'Name' => where { } ... >> +=item B<< type 'Name', where { } ... >> This creates a base type, which has no parent. @@ -1287,16 +1287,16 @@ See the L for an example of how to use these. =over 4 -=item B<< coerce 'Name' => from 'OtherName' => via { ... } >> +=item B<< coerce 'Name', from 'OtherName', via { ... } >> This defines a coercion from one type to another. The C argument is the type you are coercing I. To define multiple coercions, supply more sets of from/via pairs: - coerce 'Name' => - from 'OtherName' => via { ... }, - from 'ThirdName' => via { ... }; + coerce 'Name', + from 'OtherName', via { ... }, + from 'ThirdName', via { ... }; =item B