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       Since Type::Library 1.012, a shortcut has been available for importing
84       Type::Library and Type::Utils at the same time:
85
86         package MyType {
87           use Type::Library -base, -utils;
88
89           ...;
90         }
91
92       In Moose you might also declare types like this within classes and
93       roles too.  Unlike Moose, Type::Tiny doesn't keep types in a single
94       global flat namespace, so this doesn't work quite the same with
95       Type::Utils. It still creates the type, but it doesn't store it in any
96       type library; the type is returned.
97
98         package My::Class {
99           use Moose;
100           use Type::Utils;
101           use Types::Standard qw( Int );
102
103           my $Natural =          # store type in a variable
104             declare 'Natural',
105             as Int,
106             where { $_ > 0 };
107
108           has number => ( is => 'ro', isa => $Natural );
109         }
110
111       But really, isn't the object-oriented way cleaner?
112
113         package My::Class {
114           use Moose;
115           use Types::Standard qw( Int );
116
117           has number => (
118             is   => 'ro',
119             isa  => Int->where('$_ > 0'),
120           );
121         }
122
123   Type::Tiny and MooseX::Types
124       Types::Standard should be a drop-in replacement for MooseX::Types.  And
125       Types::Common::Numeric and Types::Common::String should easily replace
126       MooseX::Types::Common::Numeric and MooseX::Types::Common::String.
127
128       That said, if you do with to use a mixture of Type::Tiny and
129       MooseX::Types, they should fit together pretty seamlessly.
130
131         use Types::Standard qw( ArrayRef );
132         use MooseX::Types::Common::Numeric qw( PositiveInt );
133
134         # this should just work
135         my $list_of_nums = ArrayRef[PositiveInt];
136
137         # and this
138         my $list_or_num = ArrayRef | PositiveInt;
139
140   "-moose" Import Parameter
141       If you have read this far in the manual, you will know that this is the
142       usual way to import type constraints:
143
144         use Types::Standard qw( Int );
145
146       And the "Int" which is imported is a function that takes no arguments
147       and returns the Int type constraint, which is a blessed object in the
148       Type::Tiny class.
149
150       Type::Tiny mocks the Moose::Meta::TypeConstraint API so well that most
151       Moose and MooseX code will not be able to tell the difference.
152
153       But what if you need a real Moose::Meta::TypeConstraint object?
154
155         use Types::Standard -moose, qw( Int );
156
157       Now the "Int" function imported will return a genuine native Moose type
158       constraint.
159
160       This flag is mostly a throwback from when Type::Tiny native objects
161       didn't directly work in Moose. In 99.9% of cases, there is no reason to
162       use it and plenty of reasons not to. (Moose native type constraints
163       don't offer helpful methods like "plus_coercions" and "where".)
164
165   "moose_type" Method
166       Another quick way to get a native Moose type constraint object from a
167       Type::Tiny object is to call the "moose_type" method:
168
169         use Types::Standard qw( Int );
170
171         my $tiny_type   = Int;
172         my $moose_type  = $tiny_type->moose_type;
173
174       Internally, this is what the "-moose" flag makes imported functions do.
175

NEXT STEPS

177       Here's your next step:
178
179       •   Type::Tiny::Manual::UsingWithMouse
180
181           How to use Type::Tiny with Mouse, including the advantages of
182           Type::Tiny over built-in type constraints, and Mouse-specific
183           features.
184

AUTHOR

186       Toby Inkster <tobyink@cpan.org>.
187
189       This software is copyright (c) 2013-2014, 2017-2021 by Toby Inkster.
190
191       This is free software; you can redistribute it and/or modify it under
192       the same terms as the Perl 5 programming language system itself.
193

DISCLAIMER OF WARRANTIES

195       THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
196       WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
197       MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
198
199
200
201perl v5.32.1                      2021-04-T2y7pe::Tiny::Manual::UsingWithMoose(3)
Impressum