1Type::Tiny::Union(3) User Contributed Perl Documentation Type::Tiny::Union(3)
2
3
4
6 Type::Tiny::Union - union type constraints
7
9 Using via the "|" operator overload:
10
11 package Local::Stash {
12 use Moo;
13 use Types::Common qw( ArrayRef HashRef );
14
15 has data => (
16 is => 'ro',
17 isa => HashRef | ArrayRef,
18 );
19 }
20
21 my $x = Local::Stash->new( data => {} ); # ok
22 my $y = Local::Stash->new( data => [] ); # ok
23
24 Using Type::Tiny::Union's object-oriented interface:
25
26 package Local::Stash {
27 use Moo;
28 use Types::Common qw( ArrayRef HashRef );
29 use Type::Tiny::Union;
30
31 my $AnyData = Type::Tiny::Union->new(
32 name => 'AnyData',
33 type_constraints => [ HashRef, ArrayRef ],
34 );
35
36 has data => (
37 is => 'ro',
38 isa => $AnyData,
39 );
40 }
41
42 Using Type::Utils's functional interface:
43
44 package Local::Stash {
45 use Moo;
46 use Types::Common qw( ArrayRef HashRef );
47 use Type::Utils;
48
49 my $AnyData = union AnyData => [ HashRef, ArrayRef ];
50
51 has data => (
52 is => 'ro',
53 isa => $AnyData,
54 );
55 }
56
58 This module is covered by the Type-Tiny stability policy.
59
61 Union type constraints.
62
63 This package inherits from Type::Tiny; see that for most documentation.
64 Major differences are listed below:
65
66 Constructor
67 The "new" constructor from Type::Tiny still works, of course. But there
68 is also:
69
70 new_by_overload(%attributes)
71 Like the "new" constructor, but will sometimes return another type
72 constraint which is not strictly an instance of Type::Tiny::Union,
73 but still encapsulates the same meaning. This constructor is used
74 by Type::Tiny's overloading of the "|" operator.
75
76 Attributes
77 "type_constraints"
78 Arrayref of type constraints.
79
80 When passed to the constructor, if any of the type constraints in
81 the union is itself a union type constraint, this is "exploded"
82 into the new union.
83
84 "constraint"
85 Unlike Type::Tiny, you cannot pass a constraint coderef to the
86 constructor. Instead rely on the default.
87
88 "inlined"
89 Unlike Type::Tiny, you cannot pass an inlining coderef to the
90 constructor. Instead rely on the default.
91
92 "parent"
93 Unlike Type::Tiny, you cannot pass an inlining coderef to the
94 constructor. A parent will instead be automatically calculated.
95
96 "coercion"
97 You probably do not pass this to the constructor. (It's not
98 currently disallowed, as there may be a use for it that I haven't
99 thought of.)
100
101 The auto-generated default will be a Type::Coercion::Union object.
102
103 Methods
104 find_type_for($value)
105 Returns the first individual type constraint in the union which
106 $value passes.
107
108 stringifies_to($constraint)
109 See Type::Tiny::ConstrainedObject.
110
111 numifies_to($constraint)
112 See Type::Tiny::ConstrainedObject.
113
114 "with_attribute_values($attr1 => $constraint1, ...)"
115 See Type::Tiny::ConstrainedObject.
116
117 Overloading
118 • Arrayrefification calls "type_constraints".
119
121 Please report any bugs to
122 <https://github.com/tobyink/p5-type-tiny/issues>.
123
125 Type::Tiny::Manual.
126
127 Type::Tiny.
128
130 Toby Inkster <tobyink@cpan.org>.
131
133 This software is copyright (c) 2013-2014, 2017-2023 by Toby Inkster.
134
135 This is free software; you can redistribute it and/or modify it under
136 the same terms as the Perl 5 programming language system itself.
137
139 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
140 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
141 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
142
143
144
145perl v5.36.0 2023-04-24 Type::Tiny::Union(3)