p5-type-tiny
p5-type-tiny copied to clipboard
error in Type::Tiny::validate_explain for parameterized types without parents
Here's a parentless parameterized type:
#! perl
use strict;
use warnings;
package My::Types {
use Type::Library -base;
__PACKAGE__->meta->add_type(
Type::Tiny->new(
name => 'Sexagesimal',
constraint_generator => sub {
return Type::Tiny->new(
constraint => sub { 0 },
parameters => [@_],
);
},
constraint => sub {
croak( 'Sexagesimal requires parameters' );
},
),
);
}
My::Types::Sexagesimal()->of()->assert_valid( 0 );
This results in:
Can't call method "has_deep_explanation" on an undefined value at .../Type/Tiny.pm line 917.
That's in Type::Tiny::valid_explain, which doesn't check that the type has a parent before calling
has_deep_explanation on the parent:
917 if ( $self->is_parameterized and $self->parent->has_deep_explanation ) {
918 my $deep = $self->parent->deep_explanation->( $self, $value, $varname );
919 return [ $message, @$deep ] if $deep;
920 }
Why would I create a parentless parameterized type? I want the Sexagesimal type to croak if it is used as a non-parameterized type, so I have the non-parameterized constraint croak if called.
However, if the parameterized type inherits from Sexagesimal, it will call Sexagesimal's non-parameterized constraint which will croak. To avoid that, the parameterized type doesn't have a parent. (I've since set its parent to Any as a workaround.)