1Type::Tiny::IntersectioUns(e3r)Contributed Perl DocumentTaytpieo:n:Tiny::Intersection(3)
2
3
4
6 Type::Tiny::Intersection - intersection type constraints
7
9 Using via the "&" operator overload:
10
11 package Local::Stash {
12 use Moo;
13 use Types::Common qw( LowerCaseStr StrLength );
14
15 has identifier => (
16 is => 'ro',
17 isa => (LowerCaseStr) & (StrLength[4, 8]),
18 );
19 }
20
21 my $x = Local::Stash->new( data => {} ); # not ok
22 my $y = Local::Stash->new( data => [] ); # not ok
23
24 Note that it is a good idea to enclose each type being intersected in
25 parentheses to avoid Perl thinking the "&" is the sigil for a coderef.
26
27 Using Type::Tiny::Intersection's object-oriented interface:
28
29 package Local::Stash {
30 use Moo;
31 use Types::Common qw( LowerCaseStr StrLength );
32 use Type::Tiny::Intersection;
33
34 my $ShortLcStr = Type::Tiny::Intersection->new(
35 name => 'AnyData',
36 type_constraints => [ LowerCaseStr, StrLength[4, 8] ],
37 );
38
39 has identifier => (
40 is => 'ro',
41 isa => $ShortLcStr,
42 );
43 }
44
45 Using Type::Utils's functional interface:
46
47 package Local::Stash {
48 use Moo;
49 use Types::Common qw( LowerCaseStr StrLength );
50 use Type::Utils;
51
52 my $ShortLcStr = intersection ShortLcStr => [ LowerCaseStr, StrLength[4, 8] ];
53
54 has identifier => (
55 is => 'ro',
56 isa => $ShortLcStr,
57 );
58 }
59
61 This module is covered by the Type-Tiny stability policy.
62
64 Intersection type constraints.
65
66 Intersection type constraints are not often very useful. Consider the
67 intersection of HashRef and ArrayRef. A value will only pass if it is
68 both a hashref and an arrayref. Given that neither of those type
69 constraints accept "undef" or overloaded objects, there is no possible
70 value that can pass both.
71
72 Which is not to say that intersections are never useful, but it happens
73 quite rarely.
74
75 This package inherits from Type::Tiny; see that for most documentation.
76 Major differences are listed below:
77
78 Constructor
79 The "new" constructor from Type::Tiny still works, of course. But there
80 is also:
81
82 new_by_overload(%attributes)
83 Like the "new" constructor, but will sometimes return another type
84 constraint which is not strictly an instance of
85 Type::Tiny::Intersection, but still encapsulates the same meaning.
86 This constructor is used by Type::Tiny's overloading of the "&"
87 operator.
88
89 Attributes
90 "type_constraints"
91 Arrayref of type constraints.
92
93 When passed to the constructor, if any of the type constraints in
94 the intersection is itself an intersection type constraint, this is
95 "exploded" into the new intersection.
96
97 "constraint"
98 Unlike Type::Tiny, you cannot pass a constraint coderef to the
99 constructor. Instead rely on the default.
100
101 "inlined"
102 Unlike Type::Tiny, you cannot pass an inlining coderef to the
103 constructor. Instead rely on the default.
104
105 "parent"
106 Unlike Type::Tiny, you cannot pass an inlining coderef to the
107 constructor. A parent will instead be automatically calculated.
108
109 (Technically any of the types in the intersection could be treated
110 as a parent type; we choose the first arbitrarily.)
111
112 Methods
113 stringifies_to($constraint)
114 See Type::Tiny::ConstrainedObject.
115
116 numifies_to($constraint)
117 See Type::Tiny::ConstrainedObject.
118
119 "with_attribute_values($attr1 => $constraint1, ...)"
120 See Type::Tiny::ConstrainedObject.
121
122 Overloading
123 • Arrayrefification calls "type_constraints".
124
126 Please report any bugs to
127 <https://github.com/tobyink/p5-type-tiny/issues>.
128
130 Type::Tiny::Manual.
131
132 Type::Tiny.
133
134 MooseX::Meta::TypeConstraint::Intersection.
135
137 Toby Inkster <tobyink@cpan.org>.
138
140 This software is copyright (c) 2013-2014, 2017-2023 by Toby Inkster.
141
142 This is free software; you can redistribute it and/or modify it under
143 the same terms as the Perl 5 programming language system itself.
144
146 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
147 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
148 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
149
150
151
152perl v5.36.0 2023-04-24 Type::Tiny::Intersection(3)