Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error in Type::Tiny::validate_explain for parameterized types without parents #151

Open
djerius opened this issue Dec 17, 2023 · 0 comments

Comments

@djerius
Copy link
Contributor

djerius commented Dec 17, 2023

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.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant