Type::Tiny::Union(3)
π Type::Tiny::Union π
okay bestie, so this is a page from the User Contributed Perl Documentation (that's the manual name, mwah), and it's all about Type::Tiny::Union β union type constraints. no cap. let's get into it π¦
β¨ NAME β¨
Type::Tiny::Union β union type constraints, babes π
πΈ SYNOPSIS πΈ
There's like three whole ways to do this and honestly? all of them ate.
Option one: using via the "|" operator overload:
package Local::Stash {
use Moo;
use Types::Common qw( ArrayRef HashRef );
has data => (
is => 'ro',
isa => HashRef | ArrayRef,
);
}
my $x = Local::Stash->new( data => {} ); # ok
my $y = Local::Stash->new( data => [] ); # ok
Option two: using Type::Tiny::Union's object-oriented interface:
package Local::Stash {
use Moo;
use Types::Common qw( ArrayRef HashRef );
use Type::Tiny::Union;
my $AnyData = Type::Tiny::Union->new(
name => 'AnyData',
type_constraints => [ HashRef, ArrayRef ],
);
has data => (
is => 'ro',
isa => $AnyData,
);
}
Option three: using Type::Utils's functional interface:
package Local::Stash {
use Moo;
use Types::Common qw( ArrayRef HashRef );
use Type::Utils;
my $AnyData = union AnyData => [ HashRef, ArrayRef ];
has data => (
is => 'ro',
isa => $AnyData,
);
}
pick your fighter, they all slay π
β STATUS β
This module is covered by the Type-Tiny stability policy. iconic behaviour fr.
π DESCRIPTION π
Union type constraints. that's the whole vibe.
This package inherits from Type::Tiny; go read that one for most of the documentation, because the bulk of the lore lives there. The major differences are listed below: π
π Constructor π
The new constructor from Type::Tiny still works, of course, bestie. But there is also:
new_by_overload(%attributes)β like thenewconstructor, but will sometimes return another type constraint which is not strictly an instance ofType::Tiny::Union, but still encapsulates the same meaning. This constructor is used byType::Tiny's overloading of the"|"operator. so that's where the magic happens β¨
π¦ Attributes π¦
type_constraintsβ arrayref of type constraints.
When passed to the constructor, if any of the type constraints in the union is itself a union type constraint, this is "exploded" into the new union. (union-ception, but it gets flattened, don't worry π )
constraintβ unlikeType::Tiny, you cannot pass a constraint coderef to the constructor. Instead rely on the default. ngl that's just how it is πinlinedβ unlikeType::Tiny, you cannot pass an inlining coderef to the constructor. Instead rely on the default.parentβ unlikeType::Tiny, you cannot pass an inlining coderef to the constructor. A parent will instead be automatically calculated. the math is mathing π₯coercionβ you probably do not pass this to the constructor. (It's not currently disallowed, as there may be a use for it that I haven't thought of.)
The auto-generated default will be a Type::Coercion::Union object. π
π Methods π
find_type_for($value)β returns the first individual type constraint in the union which$valuepasses. first one wins, no takesies backsies.stringifies_to($constraint)β seeType::Tiny::ConstrainedObject.numifies_to($constraint)β seeType::Tiny::ConstrainedObject."with_attribute_values($attr1 => $constraint1, ...)"β seeType::Tiny::ConstrainedObject.
π§ Overloading π§
- Arrayrefification calls
type_constraints. that's it, that's the whole section, she's simple and she serves π
π BUGS π
Please report any bugs to <https://github.com/tobyink/p5-type-tiny/issues>. (not me volunteering to file the issue... okay maybe me π«Ά)
π₯ SEE ALSO π₯
Type::Tiny::Manual.Type::Tiny.
π AUTHOR π
Toby Inkster <tobyink@cpan.org>. king behaviour, truly.
π COPYRIGHT AND LICENCE π
This software is copyright (c) 2013-2014, 2017-2025 by Toby Inkster.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. so go off, share the love πΈ
π«Ά DISCLAIMER OF WARRANTIES π«Ά
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
yeah so β no promises, bestie, it is what it is ππ
and that's the whole page!! this was rendered by perl v5.42.0, dated 2026-01-17, for the manual Type::Tiny::Union(3). thanks for reading my little essay, love u, mwah πβ¨
π¦ where this page comes from
- fedora 44: perl-Type-Tiny 2.010001-2.fc44 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms