1Type::Tiny::Manual::UsiUnsgeWritChoMnotorsieb(u3t)ed PerTlypDeo:c:uTmiennyt:a:tMiaonnual::UsingWithMoose(3)
2
3
4

NAME

6       Type::Tiny::Manual::UsingWithMoose - how to use Type::Tiny with Moose
7

MANUAL

9       First read Type::Tiny::Manual::Moo, Type::Tiny::Manual::Moo2, and
10       Type::Tiny::Manual::Moo3. Everything in those parts of the manual
11       should work exactly the same in Moose.
12
13       This part of the manual will focus on Moose-specifics.
14
15   Why Use Type::Tiny At All?
16       Moose does have a built-in type constraint system which is fairly
17       convenient to use, but there are several reasons you should consider
18       using Type::Tiny instead.
19
20       ·   Type::Tiny type constraints will usually be faster than Moose
21           built-ins.  Even without Type::Tiny::XS installed, Type::Tiny
22           usually produces more efficient inline code than Moose. Coercions
23           will usually be a lot faster.
24
25       ·   Type::Tiny provides helpful methods like "where" and
26           "plus_coercions" that allow type constraints and coercions to be
27           easily tweaked on a per-attribute basis.
28
29           Something like this is much harder to do with plain Moose types:
30
31             has name => (
32               is      => "ro",
33               isa     => Str->plus_coercions(
34                 ArrayRef[Str], sub { join " ", @$_ },
35               ),
36               coerce  => 1,
37             );
38
39           Moose tends to encourage defining coercions globally, so if you
40           wanted one Str attribute to be able to coerce from ArrayRef[Str],
41           then all Str attributes would coerce from ArrayRef[Str], and they'd
42           all do that coercion in the same way. (Even if it might make sense
43           to join by a space in some places, a comma in others, and a line
44           break in others!)
45
46       ·   Type::Tiny provides automatic deep coercions, so if type Xyz has a
47           coercion, the following should "just work":
48
49             isa xyzlist => ( is => 'ro', isa => ArrayRef[Xyz], coerce => 1 );
50
51       ·   Type::Tiny offers a wider selection of built-in types.
52
53       ·   By using Type::Tiny, you can use the same type constraints and
54           coercions for attributes and method parameters, in Moose and non-
55           Moose code.
56
57   Type::Utils
58       If you've used Moose::Util::TypeConstraints, you may be accustomed to
59       using a DSL for declaring type constraints:
60
61         use Moose::Util::TypeConstraints;
62
63         subtype 'Natural',
64           as 'Int',
65           where { $_ > 0 };
66
67       There's a module called Type::Utils that provides a very similar DSL
68       for declaring types in Type::Library-based type libraries.
69
70         package My::Types {
71           use Type::Library -base;
72           use Type::Utils;
73           use Types::Standard qw( Int );
74
75           declare 'Natural',
76             as Int,
77             where { $_ > 0 };
78         }
79
80       Personally I prefer the more object-oriented way to declare types
81       though.
82
83       In Moose you might also declare types like this within classes and
84       roles too.  Unlike Moose, Type::Tiny doesn't keep types in a single
85       global flat namespace, so this doesn't work quite the same with
86       Type::Utils. It still creates the type, but it doesn't store it in any
87       type library; the type is returned.
88
89         package My::Class {
90           use Moose;
91           use Type::Utils;
92           use Types::Standard qw( Int );
93
94           my $Natural =          # store type in a variable
95             declare 'Natural',
96             as Int,
97             where { $_ > 0 };
98
99           has number => ( is => 'ro', isa => $Natural );
100         }
101
102       But really, isn't the object-oriented way cleaner?
103
104         package My::Class {
105           use Moose;
106           use Types::Standard qw( Int );
107
108           has number => (
109             is   => 'ro',
110             isa  => Int->where('$_ > 0'),
111           );
112         }
113
114   Type::Tiny and MooseX::Types
115       Types::Standard should be a drop-in replacement for MooseX::Types.  And
116       Types::Common::Numeric and Types::Common::String should easily replace
117       MooseX::Types::Common::Numeric and MooseX::Types::Common::String.
118
119       That said, if you do with to use a mixture of Type::Tiny and
120       MooseX::Types, they should fit together pretty seamlessly.
121
122         use Types::Standard qw( ArrayRef );
123         use MooseX::Types::Common::Numeric qw( PositiveInt );
124
125         # this should just work
126         my $list_of_nums = ArrayRef[PositiveInt];
127
128         # and this
129         my $list_or_num = ArrayRef | PositiveInt;
130
131   "-moose" Import Parameter
132       If you have read this far in the manual, you will know that this is the
133       usual way to import type constraints:
134
135         use Types::Standard qw( Int );
136
137       And the "Int" which is imported is a function that takes no arguments
138       and returns the Int type constraint, which is a blessed object in the
139       Type::Tiny class.
140
141       Type::Tiny mocks the Moose::Meta::TypeConstraint API so well that most
142       Moose and MooseX code will not be able to tell the difference.
143
144       But what if you need a real Moose::Meta::TypeConstraint object?
145
146         use Types::Standard -moose, qw( Int );
147
148       Now the "Int" function imported will return a genuine native Moose type
149       constraint.
150
151       This flag is mostly a throwback from when Type::Tiny native objects
152       didn't directly work in Moose. In 99.9% of cases, there is no reason to
153       use it and plenty of reasons not to. (Moose native type constraints
154       don't offer helpful methods like "plus_coercions" and "where".)
155
156   "moose_type" Method
157       Another quick way to get a native Moose type constraint object from a
158       Type::Tiny object is to call the "moose_type" method:
159
160         use Types::Standard qw( Int );
161
162         my $tiny_type   = Int;
163         my $moose_type  = $tiny_type->moose_type;
164
165       Internally, this is what the "-moose" flag makes imported functions do.
166

NEXT STEPS

168       Here's your next step:
169
170       ·   Type::Tiny::Manual::UsingWithMouse
171
172           How to use Type::Tiny with Mouse, including the advantages of
173           Type::Tiny over built-in type constraints, and Mouse-specific
174           features.
175

AUTHOR

177       Toby Inkster <tobyink@cpan.org>.
178
180       This software is copyright (c) 2013-2014, 2017-2020 by Toby Inkster.
181
182       This is free software; you can redistribute it and/or modify it under
183       the same terms as the Perl 5 programming language system itself.
184

DISCLAIMER OF WARRANTIES

186       THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
187       WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
188       MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
189
190
191
192perl v5.32.0                      2020-09-T1y7pe::Tiny::Manual::UsingWithMoose(3)
Impressum