3 package MooseX::Types::VariantTable::Declare;
10 use Sub::Exporter -setup => {
11 exports => [qw(variant_method)],
13 default => [qw(variant_method)],
17 use Moose::Meta::Method::VariantTable;
19 sub variant_method ($$$) {
20 my ( $name, $type, $body ) = @_;
24 my $meta = $class->meta;
26 my $meta_method = $class->meta->get_method($name);
28 unless ( $meta_method ) {
29 $meta_method = Moose::Meta::Method::VariantTable->new(
32 package_name => $class,
35 $meta->add_method( $name => $meta_method );
38 if ( $meta_method->isa("Moose::Meta::Method::VariantTable") ) {
39 $meta_method->add_variant( $type, $body );
41 croak "Method '$name' is already defined";
44 return $meta_method->body;
55 MooseX::Types::VariantTable::Declare - Declarative sugar for
56 L<MooseX::Types::VariantTable> based methods.
63 variant_method dance => Item => sub {
64 # Item is the least derived type in the hierarchy,
65 # every other type subtypes it
66 # this is in effect a fallback
70 # a more specific type
71 variant_method dance => Ballerina => sub {
72 my ( $self, $ballerina, @args ) = @_;
74 $ballerina; # a value that passed the TC named "Ballerina"
79 # also works with objects
80 variant_method dance => $type_object => sub { ... };
84 This module provides declarative sugar for defining
85 L<Moose::Meta::Method::VariantTable> methods in your L<Moose> classes and
88 These methods have some semantics:
92 The order of the declarations do not matter in most cases.
94 It is the type hierarchy that defines the order in which the constraints are
95 checked and items dispatched.
97 However, in the case that two constraints without an explicit relationship
98 between them (one is a subtype of the other) both accept the same value, the
99 one that was declared earlier will win. There is no way around this issue, so
100 be careful what types you use especially when mixing variants form many
103 Adding the same type to a variant table twice is an error.
107 When dispatching all of the subclass's variants will be tried before the
110 This allows shadowing of types from the superclass even using broader types.
114 ... are currently broken.
116 Don't use variant table methods in a role, unless that's the only definition,
117 because in the future variant table merging will happen at role composition
118 time in a role composition like way, so your code will not continue to work the