MooseX...ntTable(3pm)

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

πŸ’– MooseX::Types::VariantTable β€” bestie edition πŸ’–

User Contributed Perl Documentation Β· MooseX::Types::VariantTable(3pm)

okay so. picture this: you have a whole bunch of handlers and you need to pick the right one based on what type your thingy passes. that's it. that's the module. it's a type constraint based variant table and it is, genuinely, so useful. πŸŽ€


✨ NAME ✨

MooseX::Types::VariantTable β€” a variant table based on type constraints. iconic behaviour, fr fr. πŸ’…


🌸 SYNOPSIS 🌸

first up, a lil heads up that lives right at the top of the code, so we're keeping it right here too:

# see also MooseX::Types::VariantTable::Declare for a way to
# declare variant table based methods

then you load the thing:

use MooseX::Types::VariantTable;

then you build your table, and you list your variants in whatever order you want (we'll get to why that's fine in a sec):

my $dispatch_table = MooseX::Types::VariantTable->new(
    variants => [
        { type => "Foo", value => \&foo_handler },
        { type => "Bar", value => \&bar_handler },
        { type => "Item", value => \&fallback },
    ],
);

now the main character move β€” look up the correct handler for $thingy based on the type constraints it passes:

my $entry = $dispatch_table->find_variant($thingy);

orrr if all your entries are code refs, just use the dispatch convenience method and let it cook:

$dispatch_table->dispatch( $thingy, @args );

πŸ¦‹ DESCRIPTION πŸ¦‹

so this object implements a simple dispatch table that's based on Moose type constraints. no cap. πŸ’œ

here's the part that makes it chef's kiss: subtypes get checked before their parents, which means the order of the declaration does not matter. you literally cannot mess this up by shuffling your list around. the vibe check is built in. ✨

it's used internally by Moose::Meta::Method::VariantTable and MooseX::Types::VariantTable::Declare to provide primitive multi sub support. so like, it's the backbone of some very serious machinery, and it's just chilling here being cute about it. πŸ™ˆ


πŸŽ€ ATTRIBUTES πŸŽ€

ambigious_match_callback

(yes, that's how it's spelled, and no, we're not touching it β€” copy it exactly, bestie 😭)

a code reference that gets executed when find_variant found more than one matching variant for a value. it defaults to something that simply croaks with an error message like this:

Ambiguous match %s

where %s contains a list of stringified types that matched.

πŸ’€ so like, if two types both match and neither one is more specific than the other, you don't get a silent winner β€” you get a croak. which is honestly the respectful thing to do.

πŸ’… METHODS πŸ’…

  • new β€” the constructor, obvs. this is how you make the table in the first place. 🫢
  • add_variant $type, $value β€” registers $type, such that $value will be returned by find_variant for items passing $type.

subtyping is respected in the table. periodt. πŸ’š

  • find_variant $value β€” returns the registered value for the most specific type that $value passes. this is the moment. this is the main character energy. πŸ‘‘
  • dispatch $value, @args β€” a convenience method for when the registered values are code references.

calls find_variant and, if the result is a code reference, it will goto this code reference with the value and any additional arguments. ✨ (yes, goto. it's giving old school and we respect it.)

  • has_type $type β€” returns true if an entry for $type is registered. quick lil check, no notes. πŸŽ€
  • has_parent $type β€” returns true if a parent type of $type is registered. the "does your mom know about this" method, essentially. 🌸

⭐ TODO ⭐

the meta method composes in multiple inheritence but not yet with roles, due to extensibility issues with the role application code. 😭

when Moose::Meta::Role can pluggably merge methods, variant table methods can gain role composition. so like... someday. the dream is alive. πŸ¦‹


πŸ‘‘ AUTHOR πŸ‘‘

  • Yuval Kogman <nothingmuch@woobling.org>
  • Florian Ragwitz <rafl@debian.org>

absolute legends, both of them. 🫢


Copyright (c) 2008 Yuval Kogman. All rights reserved
This program is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.

and that's the whole page, bestie!! this one shipped with perl v5.36.0 and was dated 2022-12-06 β€” go forth and dispatch responsibly, ily, bye!! πŸ’–βœ¨πŸŽ€πŸ¦‹πŸŒΈπŸ’…

πŸ“¦ where this page comes from