1Type::Tiny::Manual::UsiUnsgeWritChoMnittrei(b3u)ted PerlTyDpoec:u:mTeinntya:t:iMoannual::UsingWithMite(3)
2
3
4
6 Type::Tiny::Manual::UsingWithMite - how to use Type::Tiny with Mite
7
9 Mite takes an unorthodox approach to object-oriented code. When you
10 first start a project with Mite (which we'll assume is called
11 Your::Project), Mite will create a module called Your::Project::Mite
12 for you.
13
14 Then all your classes use code like:
15
16 package Your::Project::Widget;
17
18 use Your::Project::Mite -all;
19
20 has name => (
21 is => ro,
22 isa => 'Str',
23 );
24
25 has id => (
26 is => ro,
27 isa => 'PositiveInt',
28 );
29
30 signature_for warble => (
31 named => [
32 foo => 'Int',
33 bar => 'ArrayRef',
34 ],
35 );
36
37 sub warble {
38 my ( $self, $arg ) = @_;
39 printf( "%s: %d\n", $self->name, $arg->foo );
40 return;
41 }
42
43 1;
44
45 After writing or editing each class or role, you run the command "mite
46 compile" and Mite will output a collection of compiled Perl classes
47 which have no non-core dependencies (on Perl 5.14+. There are a couple
48 of non-core dependencies on older versions of Perl.)
49
50 Attribute "isa" options are Type::Tiny type constraints expressed as
51 strings. Mite looks them up during compilation using "dwim_type" from
52 Type::Utils, and pre-loads Types::Standard, Types::Common::String, and
53 Types::Common::Numeric for you.
54
55 The "signature_for" keyword is similar to the corresponding function in
56 Type::Params. Again, note that types are expressed as strings and
57 looked up using "dwim_type".
58
59 Any types which are inlineable should work. If using coercion, any
60 coercions which are inlineable should work.
61
62 Custom Types in Mite
63 You can define your own type library (say, Your::Project::Types) using
64 Type::Library as normal:
65
66 package Your::Project::Types;
67
68 use Type::Library
69 -extends => [ 'Types::Standard', 'Types::Common::Numeric' ];
70
71 __PACKAGE__->add_type(
72 name => 'Widget',
73 parent => InstanceOf['Your::Project::Widget'],
74 )->coercion->add_type_coercions(
75 HashRef, q{Your::Project::Widget->new($_)},
76 );
77
78 __PACKAGE__->make_immutable;
79
80 1;
81
82 Now if your classes load Your::Project::Types they'll suddenly have a
83 dependency on Type::Library, so you don't get that nice zero-dependency
84 feeling. But you can add this to your ".mite/config" file:
85
86 types: Your::Project::Types
87
88 Now Mite will know to load that type library at compile time, and will
89 make those types available as stringy types everywhere.
90
91 Compiled Type Libraries
92 It does look really pretty to not have to quote your type constraints:
93
94 has name => (
95 is => ro,
96 isa => Str,
97 );
98
99 One solution for that is Type::Library::Compiler.
100
101 Say you've created the custom type library above, you can use
102 Type::Library::Compiler to compile it into a module called
103 Your::Project::Types::Compiled, which just uses Exporter and doesn't
104 rely on Type::Library or any other part of Type::Tiny.
105
106 Then your Widget class can use that:
107
108 package Your::Project::Widget;
109
110 use Your::Project::Mite -all;
111 use Your::Project::Types::Compiled -types;
112
113 has name => (
114 is => ro,
115 isa => Str,
116 );
117
118 has id => (
119 is => ro,
120 isa => PositiveInt,
121 );
122
123 signature_for warble => (
124 named => [
125 foo => Int,
126 bar => ArrayRef,
127 ],
128 );
129
130 sub warble {
131 my ( $self, $arg ) = @_;
132 printf( "%s: %d\n", $self->name, $arg->foo );
133 return;
134 }
135
136 1;
137
138 The compiled type libraries are more limited than real type libraries.
139 You can't, for example, do parameterized types with them. However, they
140 still offer some cool features like:
141
142 Foo->check( $value ) # a few basic methods like this
143 is_Foo( $value ) # boolean checks
144 assert_Foo( $value ) # assertions which die
145 Foo | Bar # unions!
146
147 This way you can write a project with object orientation, roles, method
148 modifiers, type-checked attributes, type-checked signatures, and even
149 coercion, with no non-core dependencies! (The tools like Mite and
150 Type::Library::Compiler are only needed by the developer, not the end
151 user.)
152
154 Here's your next step:
155
156 • Type::Tiny::Manual::UsingWithClassTiny
157
158 Including how to Type::Tiny in your object's "BUILD" method, and
159 third-party shims between Type::Tiny and Class::Tiny.
160
162 Toby Inkster <tobyink@cpan.org>.
163
165 This software is copyright (c) 2022-2023 by Toby Inkster.
166
167 This is free software; you can redistribute it and/or modify it under
168 the same terms as the Perl 5 programming language system itself.
169
171 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
172 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
173 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
174
175
176
177perl v5.38.0 2023-07-2T1ype::Tiny::Manual::UsingWithMite(3)