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

NAME

6       Type::Tiny::Manual::UsingWithMouse - how to use Type::Tiny with Mouse
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 Mouse.
12
13       This part of the manual will focus on Mouse-specifics.
14
15       Overall, Type::Tiny is less well-tested with Mouse than it is with
16       Moose and Moo, but there are still a good number of test cases for
17       using Type::Tiny with Mouse, and there are no known major issues with
18       Type::Tiny's Mouse support.
19
20   Why Use Type::Tiny At All?
21       Mouse does have a built-in type constraint system which is fairly
22       convenient to use, but there are several reasons you should consider
23       using Type::Tiny instead.
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 Mouse 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           Mouse 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             has 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 Mouse and non-
55           Mouse code.
56
57   Type::Utils
58       If you've used Mouse::Util::TypeConstraints, you may be accustomed to
59       using a DSL for declaring type constraints:
60
61         use Mouse::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 Mouse you might also declare types like this within classes and
84       roles too.  Unlike Mouse, 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 Mouse;
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 Mouse;
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 MouseX::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       MouseX::Types::Common::Numeric and MouseX::Types::Common::String.
118
119       That said, if you do with to use a mixture of Type::Tiny and
120       MouseX::Types, they should fit together pretty seamlessly.
121
122         use Types::Standard qw( ArrayRef );
123         use MouseX::Types::Mouse qw( Int );
124
125         # this should just work
126         my $list_of_nums = ArrayRef[Int];
127
128         # and this
129         my $list_or_num = ArrayRef | Int;
130
131   "-mouse" 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 Mouse::Meta::TypeConstraint API so well that most
142       Mouse and MouseX code will not be able to tell the difference.
143
144       But what if you need a real Mouse::Meta::TypeConstraint object?
145
146         use Types::Standard -mouse, qw( Int );
147
148       Now the "Int" function imported will return a genuine native Mouse type
149       constraint.
150
151       This flag is mostly a throwback from when Type::Tiny native objects
152       didn't directly work in Mouse. In 99.9% of cases, there is no reason to
153       use it and plenty of reasons not to. (Mouse native type constraints
154       don't offer helpful methods like "plus_coercions" and "where".)
155
156   "mouse_type" Method
157       Another quick way to get a native Mouse type constraint object from a
158       Type::Tiny object is to call the "mouse_type" method:
159
160         use Types::Standard qw( Int );
161
162         my $tiny_type   = Int;
163         my $mouse_type  = $tiny_type->mouse_type;
164
165       Internally, this is what the "-mouse" flag makes imported functions do.
166
167   Type::Tiny Performance
168       Type::Tiny should run pretty much as fast as Mouse types do. This is
169       because, when possible, it will use Mouse's XS implementations of type
170       checks to do the heavy lifting.
171
172       There are a few type constraints where Type::Tiny prefers to do things
173       without Mouse's help though, for consistency and correctness. For
174       example, the Mouse XS implementation of Bool is... strange... it
175       accepts blessed objects that overload "bool", but only if they return
176       false. If they return true, it's a type constraint error.
177
178       Using Type::Tiny instead of Mouse's type constraints shouldn't make a
179       significant difference to the performance of your code.
180

NEXT STEPS

182       Here's your next step:
183
184       •   Type::Tiny::Manual::UsingWithMite
185
186           How to use Type::Tiny with Mite, including how to write an entire
187           Perl project using clean Moose-like code and no non-core
188           dependencies.  (Not even dependencies on Mite or Type::Tiny!)
189

AUTHOR

191       Toby Inkster <tobyink@cpan.org>.
192
194       This software is copyright (c) 2013-2014, 2017-2023 by Toby Inkster.
195
196       This is free software; you can redistribute it and/or modify it under
197       the same terms as the Perl 5 programming language system itself.
198

DISCLAIMER OF WARRANTIES

200       THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
201       WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
202       MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
203
204
205
206perl v5.38.0                      2023-07-T2y1pe::Tiny::Manual::UsingWithMouse(3)
Impressum