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 the new constructor, but will sometimes return another type constraint which is not strictly an instance of Type::Tiny::Union, but still encapsulates the same meaning. This constructor is used by Type::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 β€” unlike Type::Tiny, you cannot pass a constraint coderef to the constructor. Instead rely on the default. ngl that's just how it is πŸ™ˆ
  • inlined β€” unlike Type::Tiny, you cannot pass an inlining coderef to the constructor. Instead rely on the default.
  • parent β€” unlike Type::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 $value passes. first one wins, no takesies backsies.
  • stringifies_to($constraint) β€” see Type::Tiny::ConstrainedObject.
  • numifies_to($constraint) β€” see Type::Tiny::ConstrainedObject.
  • "with_attribute_values($attr1 => $constraint1, ...)" β€” see Type::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.


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