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

BUGS

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

SEE ALSO

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

AUTHOR

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

DISCLAIMER OF WARRANTIES

214       THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
215       WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
216       MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
217
218
219
220perl v5.36.0                      2023-01-04                  Type::Library(3)
Impressum