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

NAME

6       Type::Tiny::Class - type constraints based on the "isa" method
7

SYNOPSIS

9       Using via Types::Standard:
10
11         package Local::Horse {
12           use Moo;
13           use Types::Standard qw( Str InstanceOf );
14
15           has name => (
16             is       => 'ro',
17             isa      => Str,
18           );
19
20           has owner => (
21             is       => 'ro',
22             isa      => InstanceOf[ 'Local::Person' ],
23             default  => sub { Local::Person->new },
24           );
25         }
26
27       Using Type::Tiny::Class's export feature:
28
29         package Local::Horse {
30           use Moo;
31           use Types::Standard qw( Str );
32           use Type::Tiny::Class 'Local::Person';
33
34           has name => (
35             is       => 'ro',
36             isa      => Str,
37           );
38
39           has owner => (
40             is       => 'ro',
41             isa      => LocalPerson,
42             default  => sub { LocalPerson->new },
43           );
44         }
45
46       Using Type::Tiny::Class's object-oriented interface:
47
48         package Local::Horse {
49           use Moo;
50           use Types::Standard qw( Str );
51           use Type::Tiny::Class;
52
53           my $Person = Type::Tiny::Class->new( class => 'Local::Person' );
54
55           has name => (
56             is       => 'ro',
57             isa      => Str,
58           );
59
60           has owner => (
61             is       => 'ro',
62             isa      => $Person,
63             default  => sub { $Person->new },
64           );
65         }
66
67       Using Type::Utils's functional interface:
68
69         package Local::Horse {
70           use Moo;
71           use Types::Standard qw( Str );
72           use Type::Utils;
73
74           my $Person = class_type 'Local::Person';
75
76           has name => (
77             is       => 'ro',
78             isa      => Str,
79           );
80
81           has owner => (
82             is       => 'ro',
83             isa      => $Person,
84             default  => sub { $Person->new },
85           );
86         }
87

STATUS

89       This module is covered by the Type-Tiny stability policy.
90

DESCRIPTION

92       Type constraints of the general form "{ $_->isa("Some::Class") }".
93
94       This package inherits from Type::Tiny; see that for most documentation.
95       Major differences are listed below:
96
97   Constructor
98       "new"
99           When the constructor is called on an instance of Type::Tiny::Class,
100           it passes the call through to the constructor of the class for the
101           constraint.  So for example:
102
103              my $type = Type::Tiny::Class->new(class => "Foo::Bar");
104              my $obj  = $type->new(hello => "World");
105              say ref($obj);   # prints "Foo::Bar"
106
107           This little bit of DWIM was borrowed from
108           MooseX::Types::TypeDecorator, but Type::Tiny doesn't take the idea
109           quite as far.
110
111   Attributes
112       "class"
113           The class for the constraint.
114
115       "constraint"
116           Unlike Type::Tiny, you cannot pass a constraint coderef to the
117           constructor.  Instead rely on the default.
118
119       "inlined"
120           Unlike Type::Tiny, you cannot pass an inlining coderef to the
121           constructor.  Instead rely on the default.
122
123       "parent"
124           Parent is automatically calculated, and cannot be passed to the
125           constructor.
126
127   Methods
128       "plus_constructors($source, $method_name)"
129           Much like "plus_coercions" but adds coercions that go via a
130           constructor.  (In fact, this is implemented as a wrapper for
131           "plus_coercions".)
132
133           Example:
134
135              package MyApp::Minion;
136
137              use Moose; extends "MyApp::Person";
138
139              use Types::Standard qw( HashRef Str );
140              use Type::Utils qw( class_type );
141
142              my $Person = class_type({ class => "MyApp::Person" });
143
144              has boss => (
145                 is     => "ro",
146                 isa    => $Person->plus_constructors(
147                    HashRef,     "new",
148                    Str,         "_new_from_name",
149                 ),
150                 coerce => 1,
151              );
152
153              package main;
154
155              MyApp::Minion->new(
156                 ...,
157                 boss => "Bob",  ## via MyApp::Person->_new_from_name
158              );
159
160              MyApp::Minion->new(
161                 ...,
162                 boss => { name => "Bob" },  ## via MyApp::Person->new
163              );
164
165           Because coercing "HashRef" via constructor is a common desire, if
166           you call "plus_constructors" with no arguments at all, this is the
167           default.
168
169              $classtype->plus_constructors(HashRef, "new")
170              $classtype->plus_constructors()  ## identical to above
171
172           This is handy for Moose/Mouse/Moo-based classes.
173
174       stringifies_to($constraint)
175           See Type::Tiny::ConstrainedObject.
176
177       numifies_to($constraint)
178           See Type::Tiny::ConstrainedObject.
179
180       "with_attribute_values($attr1 => $constraint1, ...)"
181           See Type::Tiny::ConstrainedObject.
182
183   Exports
184       Type::Tiny::Class can be used as an exporter.
185
186         use Type::Tiny::Class 'HTTP::Tiny';
187
188       This will export the following functions into your namespace:
189
190       "HTTPTiny"
191       is_HTTPTiny( $value )
192       assert_HTTPTiny( $value )
193       to_HTTPTiny( $value )
194
195       You will also be able to use "HTTPTiny->new(...)" as a shortcut for
196       "HTTP::Tiny->new(...)".
197
198       Multiple types can be exported at once:
199
200         use Type::Tiny::Class qw( HTTP::Tiny LWP::UserAgent );
201

BUGS

203       Please report any bugs to
204       <https://github.com/tobyink/p5-type-tiny/issues>.
205

SEE ALSO

207       Type::Tiny::Manual.
208
209       Type::Tiny.
210
211       Moose::Meta::TypeConstraint::Class.
212

AUTHOR

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

DISCLAIMER OF WARRANTIES

223       THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
224       WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
225       MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
226
227
228
229perl v5.36.0                      2023-04-24              Type::Tiny::Class(3)
Impressum