The C<Maybe[`a]> type deserves a special mention. Used by itself, it
doesn't really mean anything (and is equivalent to C<Item>). When it
is parameterized, it means that the value is either C<undef> or the
-parameterized type. So C<Maybe[Int]> means an integer or C<undef>
+parameterized type. So C<Maybe[Int]> means an integer or C<undef>.
For more details on the type hierarchy, see
L<Moose::Util::TypeConstraints>.
package, C<MyApp::Types>, which can be loaded by other classes in your
application.
+Once you're doing this, you should almost certainly look at the
+L<MooseX::Types> extension which allows easy declaration of type libraries
+and can export your types as perl constants so that you can refer to them
+as just
+
+ has 'counter' => (is => 'rw', isa => PositiveInt);
+
+rather than needing to fully qualify them everywhere. It also allows
+
+ has 'counts' => (is => 'ro', isa => HashRef[PositiveInt]);
+
+and similarly for the union and other syntax discussed below, which
+will compile time check your use of names and is generally more robust
+than the string type parsing for complex cases.
+
=head1 COERCION
One of the most powerful features of Moose's type system is its
has 'size' => (
is => 'ro',
- isa => subtype 'Int' => where { $_ > 0 },
+ isa => subtype('Int' => where { $_ > 0 }),
);
This is handy when you want to create a one-off type and don't want to
L<Moose::Util::TypeConstraints>:
class_type('MyApp::User')
- unless find_type_constraint('MyApp::User') || ;
+ unless find_type_constraint('MyApp::User');
This sort of "find or create" logic is simple to write, and will let
you work around load order issues.