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   Type library 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           Prevents new type constraints and coercions from being added to the
113           library, and also calls "$type->coercion->freeze" on every type
114           constraint in the library.
115
116           (Prior to Type::Library v2, "make_immutable" would call
117           "$type->coercion->freeze" on every constraint in the library, but
118           not prevent new type constraints and coercions from being added to
119           the library.)
120
121   Type library exported functions
122       Type libraries are exporters. For the purposes of the following
123       examples, assume that the "Types::Mine" library defines types "Number"
124       and "String".
125
126          # Exports nothing.
127          #
128          use Types::Mine;
129
130          # Exports a function "String" which is a constant returning
131          # the String type constraint.
132          #
133          use Types::Mine qw( String );
134
135          # Exports both String and Number as above.
136          #
137          use Types::Mine qw( String Number );
138
139          # Same.
140          #
141          use Types::Mine qw( :types );
142
143          # Exports "coerce_String" and "coerce_Number", as well as any other
144          # coercions
145          #
146          use Types::Mine qw( :coercions );
147
148          # Exports a sub "is_String" so that "is_String($foo)" is equivalent
149          # to "String->check($foo)".
150          #
151          use Types::Mine qw( is_String );
152
153          # Exports "is_String" and "is_Number".
154          #
155          use Types::Mine qw( :is );
156
157          # Exports a sub "assert_String" so that "assert_String($foo)" is
158          # equivalent to "String->assert_return($foo)".
159          #
160          use Types::Mine qw( assert_String );
161
162          # Exports "assert_String" and "assert_Number".
163          #
164          use Types::Mine qw( :assert );
165
166          # Exports a sub "to_String" so that "to_String($foo)" is equivalent
167          # to "String->coerce($foo)".
168          #
169          use Types::Mine qw( to_String );
170
171          # Exports "to_String" and "to_Number".
172          #
173          use Types::Mine qw( :to );
174
175          # Exports "String", "is_String", "assert_String" and "coerce_String".
176          #
177          use Types::Mine qw( +String );
178
179          # Exports everything.
180          #
181          use Types::Mine qw( :all );
182
183       Type libraries automatically inherit from Exporter::Tiny; see the
184       documentation of that module for tips and tricks importing from
185       libraries.
186
187   Type::Library's methods
188       The above sections describe the characteristics of libraries built with
189       Type::Library. The following methods are available on Type::Library
190       itself.
191
192       "setup_type_library( $package, $utils, \@extends )"
193           Sets up a package to be a type library. $utils is a boolean
194           indicating whether to import Type::Utils into the package.
195           @extends is a list of existing type libraries the package should
196           extend.
197

BUGS

199       Please report any bugs to
200       <https://github.com/tobyink/p5-type-tiny/issues>.
201

SEE ALSO

203       Type::Tiny::Manual.
204
205       Type::Tiny, Type::Utils, Types::Standard, Type::Coercion.
206
207       Moose::Util::TypeConstraints, Mouse::Util::TypeConstraints.
208

AUTHOR

210       Toby Inkster <tobyink@cpan.org>.
211
213       This software is copyright (c) 2013-2014, 2017-2023 by Toby Inkster.
214
215       This is free software; you can redistribute it and/or modify it under
216       the same terms as the Perl 5 programming language system itself.
217

DISCLAIMER OF WARRANTIES

219       THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
220       WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
221       MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
222
223
224
225perl v5.38.0                      2023-07-21                  Type::Library(3)
Impressum