List::...:Typed(3pm)

πŸ“¦
version 2.028003 Β· 4 versions
, in debian trixie, ubuntu resolute, ubuntu stonking

πŸ’– List::Objects::WithUtils::Array::Typed β€” bestie edition πŸ’–

okay so this is a User Contributed Perl Documentation page (that's the 3pm manual section, iykyk) for List::Objects::WithUtils::Array::Typed β€” the array objects that actually check your types. πŸ¦‹


✨ NAME ✨

List::Objects::WithUtils::Array::Typed β€” type-checking array objects, fr fr. πŸ‘‘


🌸 SYNOPSIS 🌸

use List::Objects::WithUtils 'array_of';

use Types::Standard -all;
use List::Objects::Types -all;

my $arr = array_of( Int() => 1 .. 10 );
$arr->push('foo');    # dies, failed type check
$arr->push(11 .. 15); # ok

my $arr_of_arrs = array_of( ArrayObj );
$arr_of_arrs->push([], []); # ok, coerces to ArrayObj

the way that push('foo') just dies on the spot?? πŸ’€ iconic behaviour honestly. and then push(11 .. 15) sails right through because those are, in fact, Ints. we love a self-aware data structure. πŸŽ€

and the last bit? passing [] and [] to an array-of-ArrayObj is totally fine because it coerces them into ArrayObjs for you. no cap. ✨


πŸ’… DESCRIPTION πŸ’…

these are type-checking array objects; elements get checked against the type you specified when the object is constructed or when new elements are added. every single time. she's thorough like that. 🌸

the first argument passed to the constructor should be a Type::Tiny type:

use Types::Standard -all;
my $arr = array_of Str() => qw/foo bar baz/;

and if the initial type-check fails, a coercion is attempted. so like, she's strict but she's not unreasonable. 🫢

this class consumes the following roles, which contain most of the relevant documentation:

  • List::Objects::WithUtils::Role::Array
  • List::Objects::WithUtils::Role::Array::WithJunctions
  • List::Objects::WithUtils::Role::Array::Typed

also see Types::Standard, List::Objects::Types β€” go read those too, bestie, they're literally where the good stuff lives. πŸ“šβœ¨


πŸŽ€ AUTHOR πŸŽ€

Jon Portnoy <avenj@cobaltirc.org> with significant contributions from Toby Inkster (CPAN: TOBYINK). πŸ’– shoutout to the legends, we are obsessed. 🫢


and that's the whole page!! you read all of it, so proud of you. 🌈

this cutie was documented for perl v5.38.2, dated 2024-03-07 β€” go forth and type-check your arrays, main character. πŸ’…βœ¨

πŸ“¦ where this page comes from