1Type::Library(3)      User Contributed Perl Documentation     Type::Library(3)
2
3
4

NAME

6       Type::Library - tiny, yet Moo(se)-compatible type libraries
7

SYNOPSIS

9          package Types::Mine {
10             use Scalar::Util qw(looks_like_number);
11             use Type::Library -base;
12             use Type::Tiny;
13
14             my $NUM = "Type::Tiny"->new(
15                name       => "Number",
16                constraint => sub { looks_like_number($_) },
17                message    => sub { "$_ ain't a number" },
18             );
19
20             __PACKAGE__->meta->add_type($NUM);
21
22             __PACKAGE__->meta->make_immutable;
23          }
24
25          package Ermintrude {
26             use Moo;
27             use Types::Mine qw(Number);
28             has favourite_number => (is => "ro", isa => Number);
29          }
30
31          package Bullwinkle {
32             use Moose;
33             use Types::Mine qw(Number);
34             has favourite_number => (is => "ro", isa => Number);
35          }
36
37          package Maisy {
38             use Mouse;
39             use Types::Mine qw(Number);
40             has favourite_number => (is => "ro", isa => Number);
41          }
42

STATUS

44       This module is covered by the Type-Tiny stability policy.
45

DESCRIPTION

47       Type::Library is a tiny class for creating MooseX::Types-like type
48       libraries which are compatible with Moo, Moose and Mouse.
49
50       If you're reading this because you want to create a type library, then
51       you're probably better off reading Type::Tiny::Manual::Libraries.
52
53   Methods
54       A type library is a singleton class. Use the "meta" method to get a
55       blessed object which other methods can get called on. For example:
56
57          Types::Mine->meta->add_type($foo);
58
59       "add_type($type)" or "add_type(%opts)"
60           Add a type to the library. If %opts is given, then this method
61           calls "Type::Tiny->new(%opts)" first, and adds the resultant type.
62
63           Adding a type named "Foo" to the library will automatically define
64           four functions in the library's namespace:
65
66           "Foo"
67               Returns the Type::Tiny object.
68
69           "is_Foo($value)"
70               Returns true iff $value passes the type constraint.
71
72           "assert_Foo($value)"
73               Returns $value iff $value passes the type constraint. Dies
74               otherwise.
75
76           "to_Foo($value)"
77               Coerces the value to the type.
78
79       "get_type($name)"
80           Gets the "Type::Tiny" object corresponding to the name.
81
82       "has_type($name)"
83           Boolean; returns true if the type exists in the library.
84
85       "type_names"
86           List all types defined by the library.
87
88       "add_coercion($c)" or "add_coercion(%opts)"
89           Add a standalone coercion to the library. If %opts is given, then
90           this method calls "Type::Coercion->new(%opts)" first, and adds the
91           resultant coercion.
92
93           Adding a coercion named "FooFromBar" to the library will
94           automatically define a function in the library's namespace:
95
96           "FooFromBar"
97               Returns the Type::Coercion object.
98
99       "get_coercion($name)"
100           Gets the "Type::Coercion" object corresponding to the name.
101
102       "has_coercion($name)"
103           Boolean; returns true if the coercion exists in the library.
104
105       "coercion_names"
106           List all standalone coercions defined by the library.
107
108       "import(@args)"
109           Type::Library-based libraries are exporters.
110
111       "make_immutable"
112           A shortcut for calling "$type->coercion->freeze" on every type
113           constraint in the library.
114
115   Constants
116       "NICE_PROTOTYPES"
117           If this is true, then Type::Library will give parameterizable type
118           constraints slightly the nicer prototype of "(;$)" instead of the
119           default "(;@)".  This allows constructs like:
120
121              ArrayRef[Int] | HashRef[Int]
122
123           ... to "just work".
124
125   Export
126       Type libraries are exporters. For the purposes of the following
127       examples, assume that the "Types::Mine" library defines types "Number"
128       and "String".
129
130          # Exports nothing.
131          #
132          use Types::Mine;
133
134          # Exports a function "String" which is a constant returning
135          # the String type constraint.
136          #
137          use Types::Mine qw( String );
138
139          # Exports both String and Number as above.
140          #
141          use Types::Mine qw( String Number );
142
143          # Same.
144          #
145          use Types::Mine qw( :types );
146
147          # Exports "coerce_String" and "coerce_Number", as well as any other
148          # coercions
149          #
150          use Types::Mine qw( :coercions );
151
152          # Exports a sub "is_String" so that "is_String($foo)" is equivalent
153          # to "String->check($foo)".
154          #
155          use Types::Mine qw( is_String );
156
157          # Exports "is_String" and "is_Number".
158          #
159          use Types::Mine qw( :is );
160
161          # Exports a sub "assert_String" so that "assert_String($foo)" is
162          # equivalent to "String->assert_return($foo)".
163          #
164          use Types::Mine qw( assert_String );
165
166          # Exports "assert_String" and "assert_Number".
167          #
168          use Types::Mine qw( :assert );
169
170          # Exports a sub "to_String" so that "to_String($foo)" is equivalent
171          # to "String->coerce($foo)".
172          #
173          use Types::Mine qw( to_String );
174
175          # Exports "to_String" and "to_Number".
176          #
177          use Types::Mine qw( :to );
178
179          # Exports "String", "is_String", "assert_String" and "coerce_String".
180          #
181          use Types::Mine qw( +String );
182
183          # Exports everything.
184          #
185          use Types::Mine qw( :all );
186
187       Type libraries automatically inherit from Exporter::Tiny; see the
188       documentation of that module for tips and tricks importing from
189       libraries.
190

BUGS

192       Please report any bugs to
193       <https://github.com/tobyink/p5-type-tiny/issues>.
194

SEE ALSO

196       Type::Tiny::Manual.
197
198       Type::Tiny, Type::Utils, Types::Standard, Type::Coercion.
199
200       Moose::Util::TypeConstraints, Mouse::Util::TypeConstraints.
201

AUTHOR

203       Toby Inkster <tobyink@cpan.org>.
204
206       This software is copyright (c) 2013-2014, 2017-2021 by Toby Inkster.
207
208       This is free software; you can redistribute it and/or modify it under
209       the same terms as the Perl 5 programming language system itself.
210

DISCLAIMER OF WARRANTIES

212       THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
213       WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
214       MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
215
216
217
218perl v5.34.0                      2021-09-13                  Type::Library(3)
Impressum