MooseX...eclare(3pm)

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

✨ MooseX::Types::VariantTable::Declare ✨

okay bestie so this is MooseX::Types::VariantTable::Declare from the User Contributed Perl Documentation crew, and it's literally the declarative sugar you sprinkle on top of MooseX::Types::VariantTable based methods so you don't have to build them by hand like some kind of Victorian orphan πŸ’…


🌸 NAME 🌸

MooseX::Types::VariantTable::Declare β€” Declarative sugar for MooseX::Types::VariantTable based methods. that's it that's the whole vibe πŸ“


πŸŽ€ SYNOPSIS πŸŽ€

here's the tea, look at how cute this is:

package Awesome;
use Moose;

variant_method dance => Item => sub {
    # Item is the least derived type in the hierarchy,
    # every other type subtypes it
    # this is in effect a fallback
    return "fallback";
};

# a more specific type
variant_method dance => Ballerina => sub {
    my ( $self, $ballerina, @args ) = @_;

    $ballerina; # a value that passed the TC named "Ballerina"

    return "pretty!";
};

# also works with objects
variant_method dance => $type_object => sub { ... };

like. obsessed. you write the sub, it figures out the dispatch. and yes it works with objects too, not just type names β€” $type_object is right there doing its little thing πŸ¦‹


πŸ’– DESCRIPTION πŸ’–

so this module hands you declarative sugar for defining Moose::Meta::Method::VariantTable methods inside your Moose classes and roles. it's giving convenience, it's giving elegance, it's giving "why did I ever write this by hand" 😭

these methods come with some semantics though, and bestie you need to know them, so let's go:

πŸ‘‘ Declaration

okay so, the order you declare things in does not matter in most cases. like, it's not a first-come-first-serve situation, it's a whole system.

it's the type hierarchy that decides the order in which the constraints get checked and the items get dispatched. the types run the show, periodt.

but β€” and this is the important but β€” if two constraints have no explicit relationship between them (meaning neither one is a subtype of the other) and they both accept the same value, then the one that was declared earlier wins. there is genuinely no way around this issue, so be careful what types you use, especially when you're mixing variants from many different sources. it's giving chaos if you're not paying attention πŸ’€

adding the same type to a variant table twice is an error. don't do it. it's not a fun surprise, it's just an error.

🌈 Inheritence

when dispatching, all of the subclass's variants get tried before the superclass. every single one. no skipping.

this means you can shadow types from the superclass β€” even using broader types to do it. the subclass always gets its moment first, which is honestly main character energy if you think about it πŸ‘‘

πŸ™ˆ Roles

... are currently broken. 😭

don't use variant table methods in a role, unless that's the only definition β€” because in the future, variant table merging is gonna happen at role composition time in a role-composition kind of way, so your code will not continue to work the same. it's a glow-up that's coming for your code whether your code is ready or not. so like. plan accordingly 🫢


that's the whole page, bestie, you ate it. πŸ’…βœ¨

this was perl v5.36.0, dated 2022-12-06, filed under MooseX...eclare(3pm) β€” mwah, go forth and dispatch πŸŽ€πŸ¦‹πŸŒΈ

πŸ“¦ where this page comes from