Commit | Line | Data |
ecd7cc3e |
1 | =pod |
2 | |
3 | =head1 NAME |
4 | |
d67ce58f |
5 | Moose::Manual::Types - Moose's type system |
ecd7cc3e |
6 | |
7 | =head1 TYPES IN PERL? |
8 | |
0c551c67 |
9 | Moose provides its own type system for attributes. You can also use |
10 | these types to validate method parameters with the help of a MooseX |
11 | module. |
ecd7cc3e |
12 | |
13 | Moose's type system is based on a combination of Perl 5's own |
0c551c67 |
14 | I<implicit> types and some Perl 6 concepts. You can easily create your |
15 | own subtypes with custom constraints, making it easy to express any |
16 | sort of validation. |
ecd7cc3e |
17 | |
0c551c67 |
18 | Types have names, and you can re-use them by name, making it easy to |
ecd7cc3e |
19 | share types throughout a large application. |
20 | |
21 | Let us be clear that is not a "real" type system. Moose does not |
0c551c67 |
22 | magically make Perl start associating types with variables. This is |
23 | just an advanced parameter checking system which allows you to |
24 | associate a name with a constraint. |
ecd7cc3e |
25 | |
26 | That said, it's still pretty damn useful, and we think it's one of the |
27 | things that makes Moose both fun and powerful. Taking advantage of the |
28 | type system makes it much easier to ensure that you are getting valid |
29 | data, and it also contributes greatly to code maintainability. |
30 | |
31 | =head1 THE TYPES |
32 | |
33 | The basic Moose type hierarchy looks like this |
34 | |
35 | Any |
36 | Item |
37 | Bool |
38 | Maybe[`a] |
39 | Undef |
40 | Defined |
41 | Value |
42 | Num |
43 | Int |
44 | Str |
45 | ClassName |
7612cc01 |
46 | RoleName |
ecd7cc3e |
47 | Ref |
48 | ScalarRef |
49 | ArrayRef[`a] |
50 | HashRef[`a] |
51 | CodeRef |
52 | RegexpRef |
53 | GlobRef |
54 | FileHandle |
55 | Object |
56 | Role |
57 | |
58 | In practice, the only difference between C<Any> and C<Item> is |
59 | conceptual. C<Item> is used as the top-level type in the hierarchy. |
60 | |
61 | The rest of these types correspond to existing Perl concepts. For |
62 | example, a C<Num> is anything that Perl thinks looks like a number. An |
63 | C<Object> is a blessed reference, etc. |
64 | |
65 | The types followed by "[`a]" can be parameterized. So instead of just |
66 | plain C<ArrayRef> we can say that we want C<ArrayRef[Int]> instead. We |
67 | can even do something like C<HashRef[ArrayRef[Str]]>. |
68 | |
69 | The C<Maybe[`a]> type deserves a special mention. Used by itself, it |
70 | doesn't really mean anything (and is equivalent to C<Item>). When it |
71 | is parameterized, it means that the value is either C<undef> or the |
72 | parameterized type. So C<Maybe[Int]> means an integer or C<undef> |
73 | |
74 | For more details on the type hierarchy, see |
75 | L<Moose::Util::TypeConstraints>. |
76 | |
77 | =head1 WHAT IS A TYPE? |
78 | |
79 | It's important to realize that types are not classes (or |
80 | packages). Types are just objects (L<Moose::Meta::TypeConstraint> |
0c551c67 |
81 | objects, to be exact) with a name and a constraint. Moose maintains a |
6549b0d1 |
82 | global type registry that lets it convert names like C<Num> into the |
0c551c67 |
83 | appropriate object. |
ecd7cc3e |
84 | |
85 | However, class names I<can be> type names. When you define a new class |
86 | using Moose, it defines an associated type name behind the scenes: |
87 | |
88 | package MyApp::User; |
89 | |
90 | use Moose; |
91 | |
92 | Now you can use C<'MyApp::User'> as a type name: |
93 | |
94 | has creator => ( |
6c5b976f |
95 | is => 'ro', |
ecd7cc3e |
96 | isa => 'MyApp::User', |
97 | ); |
98 | |
99 | However, for non-Moose classes there's no magic. You may have to |
100 | explicitly declare the class type. This is a bit muddled because Moose |
101 | assumes that any unknown type name passed as the C<isa> value for an |
102 | attribute is a class. So this works: |
103 | |
104 | has 'birth_date' => ( |
6c5b976f |
105 | is => 'ro', |
ecd7cc3e |
106 | isa => 'DateTime', |
107 | ); |
108 | |
109 | In general, when Moose is presented with an unknown name, it assumes |
110 | that the name is a class: |
111 | |
112 | subtype 'ModernDateTime' |
113 | => as 'DateTime' |
114 | => where { $_->year() >= 1980 } |
115 | => message { 'The date you provided is not modern enough' }; |
116 | |
117 | has 'valid_dates' => ( |
118 | is => 'ro', |
119 | isa => 'ArrayRef[DateTime]', |
120 | ); |
121 | |
0c39debe |
122 | Moose will assume that C<DateTime> is a class name in both of these |
0c551c67 |
123 | instances. |
ecd7cc3e |
124 | |
125 | =head1 SUBTYPES |
126 | |
127 | Moose uses subtypes in its built-in hierarchy. C<Int> is a child of |
128 | C<Num> for example. |
129 | |
130 | A subtype is defined in terms of a parent type and a constraint. Any |
131 | constraints defined by the parent(s) will be checked first, and then |
0c39debe |
132 | the the subtype's. A value must pass I<all> of these checks to be |
0c551c67 |
133 | valid for the subtype. |
ecd7cc3e |
134 | |
0c551c67 |
135 | Typically, a subtype takes the parent's constraint and makes it more |
ecd7cc3e |
136 | specific. |
137 | |
138 | A subtype can also define its own constraint failure message. This |
139 | lets you do things like have an error "The value you provided (20), |
140 | was not a valid rating, which must be a number from 1-10." This is |
141 | much friendlier than the default error, which just says that the value |
142 | failed a validation check for the type. |
143 | |
144 | Here's a simple (and useful) subtype example: |
145 | |
146 | subtype 'PositiveInt' |
147 | => as 'Int' |
148 | => where { $_ > 0 } |
149 | => message { "The number you provided, $_, was not a positive number" } |
150 | |
151 | Note that the sugar functions for working with types are all exported |
152 | by L<Moose::Util::TypeConstraints>. |
153 | |
d67ce58f |
154 | =head2 Creating a new type (that isn't a subtype) |
ecd7cc3e |
155 | |
156 | You can also create new top-level types: |
157 | |
158 | type 'FourCharacters' => where { defined $_ && length $_ == 4 }; |
159 | |
0c551c67 |
160 | In practice, this example is more or less the same as subtyping |
636f25f3 |
161 | C<Str>, except you have to check definedness yourself. |
ecd7cc3e |
162 | |
163 | It's hard to find a case where you wouldn't want to subtype a very |
164 | broad type like C<Defined>, C<Ref> or C<Object>. |
165 | |
0c551c67 |
166 | Defining a new top-level type is conceptually the same as subtyping |
167 | C<Item>. |
ecd7cc3e |
168 | |
169 | =head1 TYPE NAMES |
170 | |
171 | Type names are global throughout the current Perl |
0c551c67 |
172 | interpreter. Internally, Moose maps names to type objects via a |
173 | L<registry|Moose::Meta::TypeConstraint::Registry>. |
ecd7cc3e |
174 | |
175 | If you have multiple apps or libraries all using Moose in the same |
176 | process, you could have problems with collisions. We recommend that |
177 | you prefix names with some sort of namespace indicator to prevent |
178 | these sorts of collisions. |
179 | |
180 | For example, instead of calling a type "PositiveInt", call it |
bcc22289 |
181 | "MyApp::Type::PositiveInt" or "MyApp::Types::PositiveInt". We |
182 | recommend that you centralize all of these definitions in a single |
183 | package, C<MyApp::Types>, which can be loaded by other classes in your |
184 | application. |
ecd7cc3e |
185 | |
186 | =head1 COERCION |
187 | |
188 | One of the most powerful features of Moose's type system is its |
0c551c67 |
189 | coercions. A coercion is a way to convert from one type to another. |
ecd7cc3e |
190 | |
191 | subtype 'ArrayRefOfInts' |
192 | => as 'ArrayRef[Int]'; |
193 | |
194 | coerce 'ArrayRefOfInts' |
195 | => from 'Int' |
196 | => via { [ $_ ] }; |
197 | |
198 | You'll note that we had to create a subtype rather than coercing |
199 | C<ArrayRef[Int]> directly. This is just a quirk of how Moose |
200 | works. |
201 | |
202 | Coercions, like type names, are global. This is I<another> reason why |
203 | it is good to namespace your types. Moose will I<never> try to coerce |
204 | a value unless you explicitly ask for it. This is done by setting the |
16fb3624 |
205 | C<coerce> attribute option to a true value: |
ecd7cc3e |
206 | |
207 | package Foo; |
208 | |
209 | has 'sizes' => ( |
6c5b976f |
210 | is => 'ro', |
ecd7cc3e |
211 | isa => 'ArrayRefOfInts', |
212 | coerce => 1, |
213 | ); |
214 | |
215 | Foo->new( sizes => 42 ); |
216 | |
217 | This code example will do the right thing, and the newly created |
218 | object will have C<[ 42 ]> as its C<sizes> attribute. |
219 | |
d67ce58f |
220 | =head2 Deep coercion |
ecd7cc3e |
221 | |
222 | Deep coercion is the coercion of type parameters for parameterized |
223 | types. Let's take these types as an example: |
224 | |
225 | subtype 'HexNum' |
226 | => as 'Str' |
227 | => where { /[a-f0-9]/i }; |
228 | |
229 | coerce 'Int' |
230 | => from 'HexNum' |
231 | => via { hex $_ }; |
232 | |
233 | has 'sizes' => ( |
6c5b976f |
234 | is => 'ro', |
ecd7cc3e |
235 | isa => 'ArrayRef[Int]', |
236 | coerce => 1, |
237 | ); |
238 | |
239 | If we try passing an array reference of hex numbers for the C<sizes> |
0c551c67 |
240 | attribute, Moose will not do any coercion. |
ecd7cc3e |
241 | |
0c551c67 |
242 | However, you can define a set of subtypes to enable coercion between |
243 | two parameterized types. |
ecd7cc3e |
244 | |
245 | subtype 'ArrayRefOfHexNums' |
246 | => as 'ArrayRef[HexNum]'; |
247 | |
248 | subtype 'ArrayRefOfInts' |
249 | => as 'ArrayRef[Int]'; |
250 | |
251 | coerce 'ArrayRefOfInts' |
252 | => from 'ArrayRefOfHexNums' |
253 | => via { [ map { hex } @{$_} ] }; |
254 | |
255 | Foo->new( sizes => [ 'a1', 'ff', '22' ] ); |
256 | |
257 | Now Moose will coerce the hex numbers to integers. |
258 | |
0c551c67 |
259 | However, Moose does not attempt to chain coercions, so it will not |
260 | coerce a single hex number. To do that, we need to define a separate |
261 | coercion: |
ecd7cc3e |
262 | |
263 | coerce 'ArrayRefOfInts' |
264 | => from 'HexNum' |
265 | => via { [ hex $_ ] }; |
266 | |
267 | Yes, this can all get verbose, but coercion is tricky magic, and we |
0c551c67 |
268 | think it's best to make it explicit. |
ecd7cc3e |
269 | |
270 | =head1 TYPE UNIONS |
271 | |
272 | Moose allows you to say that an attribute can be of two or more |
273 | disparate types. For example, we might allow an C<Object> or |
274 | C<FileHandle>: |
275 | |
276 | has 'output' => ( |
277 | is => 'rw', |
278 | isa => 'Object | FileHandle', |
279 | ); |
280 | |
281 | Moose actually parses that string and recognizes that you are creating |
282 | a type union. The C<output> attribute will accept any sort of object, |
283 | as well as an unblessed file handle. It is up to you to do the right |
284 | thing for each of them in your code. |
285 | |
0c551c67 |
286 | Whenever you use a type union, you should consider whether or not |
287 | coercion might be a better answer. |
ecd7cc3e |
288 | |
289 | For our example above, we might want to be more specific, and insist |
290 | that output be an object with a C<print> method: |
291 | |
292 | subtype 'CanPrint' |
293 | => as 'Object' |
294 | => where { $_->can('print') }; |
295 | |
296 | We can coerce file handles to an object that satisfies this condition |
297 | with a simple wrapper class: |
298 | |
299 | package FHWrapper; |
300 | |
301 | use Moose; |
302 | |
303 | has 'handle' => ( |
6c5b976f |
304 | is => 'rw', |
ecd7cc3e |
305 | isa => 'FileHandle', |
306 | ); |
307 | |
308 | sub print { |
309 | my $self = shift; |
310 | my $fh = $self->handle(); |
311 | |
312 | print $fh @_; |
313 | } |
314 | |
315 | Now we can define a coercion from C<FileHandle> to our wrapper class: |
316 | |
0c551c67 |
317 | coerce 'CanPrint' |
ecd7cc3e |
318 | => from 'FileHandle' |
319 | => via { FHWrapper->new( handle => $_ ) }; |
320 | |
321 | has 'output' => ( |
322 | is => 'rw', |
323 | isa => 'CanPrint', |
324 | coerce => 1, |
325 | ); |
326 | |
0c551c67 |
327 | This pattern of using a coercion instead of a type union will help |
328 | make your class internals simpler. |
ecd7cc3e |
329 | |
330 | =head1 TYPE CREATION HELPERS |
331 | |
332 | The L<Moose::Util::TypeConstraints> module exports a number of helper |
333 | functions for creating specific kinds of types. These include |
334 | C<class_type>, C<role_type>, and C<maybe_type>. See the docs for |
335 | details. |
336 | |
337 | One helper worth noting is C<enum>, which allows you to create a |
338 | subtype of C<Str> that only allows the specified values: |
339 | |
340 | enum 'RGB' => qw( red green blue ); |
341 | |
342 | This creates a type named C<RGB> |
343 | |
344 | =head1 ANONYMOUS TYPES |
345 | |
346 | All of the type creation functions return a type object. This type |
347 | object can be used wherever you would use a type name, as a parent |
16fb3624 |
348 | type, or as the value for an attribute's C<isa> option: |
ecd7cc3e |
349 | |
350 | has 'size' => ( |
6c5b976f |
351 | is => 'ro', |
ecd7cc3e |
352 | isa => subtype 'Int' => where { $_ > 0 }, |
353 | ); |
354 | |
355 | This is handy when you want to create a one-off type and don't want to |
356 | "pollute" the global namespace registry. |
357 | |
358 | =head1 VALIDATING METHOD PARAMETERS |
359 | |
360 | Moose does not provide any means of validating method |
361 | parameters. However, there are several MooseX extensions on CPAN which |
362 | let you do this. |
363 | |
0c39debe |
364 | The simplest and least sugary is L<MooseX::Params::Validate>. This |
ecd7cc3e |
365 | lets you validate a set of named parameters using Moose types: |
366 | |
367 | use Moose; |
368 | use MooseX::Params::Validate; |
369 | |
370 | sub foo { |
371 | my $self = shift; |
0c551c67 |
372 | my %params = validated_hash( |
ecd7cc3e |
373 | \@_, |
374 | bar => { isa => 'Str', default => 'Moose' }, |
375 | ); |
376 | ... |
377 | } |
378 | |
0c39debe |
379 | L<MooseX::Params::Validate> also supports coercions. |
ecd7cc3e |
380 | |
381 | There are several more powerful extensions that support method |
382 | parameter validation using Moose types, including |
0c39debe |
383 | L<MooseX::Method::Signatures>, which gives you a full-blown C<method> |
ecd7cc3e |
384 | keyword. |
385 | |
386 | method morning (Str $name) { |
387 | $self->say("Good morning ${name}!"); |
388 | } |
389 | |
ca680d1f |
390 | =head1 LOAD ORDER ISSUES |
391 | |
0c551c67 |
392 | Because Moose types are defined at runtime, you may run into load |
393 | order problems. In particular, you may want to use a class's type |
394 | constraint before that type has been defined. |
ca680d1f |
395 | |
0c551c67 |
396 | We have several recommendations for ameliorating this problem. First, |
397 | define I<all> of your custom types in one module, |
398 | C<MyApp::Types>. Second, load this module in all of your other |
399 | modules. |
ca680d1f |
400 | |
401 | If you are still having load order problems, you can make use of the |
402 | C<find_type_constraint> function exported by |
403 | L<Moose::Util::TypeConstraints>: |
404 | |
0c551c67 |
405 | class_type('MyApp::User') |
406 | unless find_type_constraint('MyApp::User') || ; |
ca680d1f |
407 | |
408 | This sort of "find or create" logic is simple to write, and will let |
409 | you work around load order issues. |
410 | |
ecd7cc3e |
411 | =head1 AUTHOR |
412 | |
413 | Dave Rolsky E<lt>autarch@urth.orgE<gt> |
414 | |
415 | =head1 COPYRIGHT AND LICENSE |
416 | |
2840a3b2 |
417 | Copyright 2009 by Infinity Interactive, Inc. |
ecd7cc3e |
418 | |
419 | L<http://www.iinteractive.com> |
420 | |
421 | This library is free software; you can redistribute it and/or modify |
422 | it under the same terms as Perl itself. |
423 | |
424 | =cut |