Type::T...section(3)

✨ NAME ✨

Type::Tiny::Intersection β€” intersection type constraints πŸ’–

(so this is a page from the User Contributed Perl Documentation, bestie, and it's all about one lil module πŸŽ€)


πŸ¦‹ SYNOPSIS πŸ¦‹

okay so there are three whole ways to do this and we're gonna look at all of them, no skipping ✨

Way 1: using the & operator overload

package Local::Stash {
  use Moo;
  use Types::Common qw( LowerCaseStr StrLength );

  has identifier => (
    is   => 'ro',
    isa  => (LowerCaseStr) & (StrLength[4, 8]),
  );
}

my $x = Local::Stash->new( data => {} );  # not ok
my $y = Local::Stash->new( data => [] );  # not ok
tiny lil heads-up bestie: it is a good idea to enclose each type being intersected in parentheses, so Perl doesn't think the & is the sigil for a coderef. she WILL get confused. protect her. πŸ™ˆ

Way 2: the object-oriented interface of Type::Tiny::Intersection

package Local::Stash {
  use Moo;
  use Types::Common qw( LowerCaseStr StrLength );
  use Type::Tiny::Intersection;

  my $ShortLcStr = Type::Tiny::Intersection->new(
    name             => 'AnyData',
    type_constraints => [ LowerCaseStr, StrLength[4, 8] ],
  );

  has identifier => (
    is   => 'ro',
    isa  => $ShortLcStr,
  );
}

Way 3: the functional interface from Type::Utils

package Local::Stash {
  use Moo;
  use Types::Common qw( LowerCaseStr StrLength );
  use Type::Utils;

  my $ShortLcStr = intersection ShortLcStr => [ LowerCaseStr, StrLength[4, 8] ];

  has identifier => (
    is   => 'ro',
    isa  => $ShortLcStr,
  );
}

πŸ’… STATUS πŸ’…

This module is covered by the Type-Tiny stability policy. (she's official. she's documented. she's fine. periodt. πŸ‘‘)


🌸 DESCRIPTION 🌸

Intersection type constraints. 🫢

ngl bestie, intersection type constraints are not often very useful. like, imagine the intersection of HashRef and ArrayRef. a value only passes if it is both a hashref and an arrayref. and given that neither of those type constraints accept undef or overloaded objects... there is no possible value that can pass both. πŸ’€ it's giving impossible. it's giving empty set.

which is not to say that intersections are never useful β€” it's just that it happens quite rarely. rare queen behaviour. πŸ¦‹

This package inherits from Type::Tiny; go read that one for most of the documentation. the major differences are listed below:


πŸŽ€ Constructor πŸŽ€

The new constructor from Type::Tiny still works, obviously, she's not going anywhere. But there's also:

  • new_by_overload(%attributes) β€” just like the new constructor, but sometimes she'll hand you back another type constraint which is not strictly an instance of Type::Tiny::Intersection, but still encapsulates the exact same meaning. this is the constructor that Type::Tiny's overloading of the & operator uses. iykyk ✨

🧚 Attributes 🧚

  • type_constraints β€” arrayref of type constraints. πŸ’– when it's passed to the constructor, if any of the type constraints in the intersection is itself an intersection type constraint, it gets "exploded" into the new intersection. boom. nested intersections, flattened. slay.
  • constraint β€” unlike Type::Tiny, you cannot pass a constraint coderef to the constructor. instead, rely on the default. that's the vibe. πŸ™ˆ
  • inlined β€” unlike Type::Tiny, you cannot pass an inlining coderef to the constructor. instead, rely on the default. same energy. ✨
  • parent β€” unlike Type::Tiny, you cannot pass an inlining coderef to the constructor. a parent will instead be automatically calculated. (technically, any of the types in the intersection could be treated as a parent type; we choose the first one arbitrarily. chaos but make it consistent. πŸ’…)

πŸ“ Methods πŸ“

  • 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. πŸ’…

😭 BUGS 😭

Please report any bugs to <https://github.com/tobyink/p5-type-tiny/issues>. bestie if you find one, go tell Toby, he's lovely and he wants to know 🫢


πŸ”₯ SEE ALSO πŸ”₯

  • Type::Tiny::Manual.
  • Type::Tiny.
  • MooseX::Meta::TypeConstraint::Intersection.

πŸ‘‘ AUTHOR πŸ‘‘

Toby Inkster <tobyink@cpan.org>. legend behaviour. ate. πŸ’–


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. 🌸


πŸ’€ 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.

like yeah no promises bestie, she comes with no warranty, take her as she is πŸ™ˆ


and that's the whole page fr fr ✨ perl v5.42.0, dated 2026-01-17, from the Type::T...section(3) of your local manual β€” mwah, go forth and intersect responsibly πŸ’…πŸ’–

πŸ“¦ where this page comes from