如何在 Type::Tiny::_build_coercion 中避免 "deep recursion" error/warning

How to avoid "deep recursion" error/warning in Type::Tiny::_build_coercion

我继承了以下针对 Type-Tiny-1.004004 编写和工作的代码:

package Company::Types;

use Type::Library -base, -declare => qw< TruncatedString >;
use Type::Utils -all;

declare TruncatedString, as Str,
    where { length $_ },
    inline_as {
        my ($constraint, $varname) = @_;
        
        return sprintf ( '!defined %s or %s',
            $varname,
            $constraint->parent->inline_check($varname),
        );
    },
    constraint_generator => sub {
        my ($max) = @_;
        die q{Max length must be positive!} unless int($max) > 0;
        return sub { 
            (length $_) <= $max;
        };
    },
    inline_generator => sub {
        my ($max) = @_;
        return sub { 
            my ($constraint, $varname) = @_;

            return sprintf(
                '%s and length %s <= %d', 
                $constraint->parent->inline_check($varname),
                $varname,
                $max,
            );
        };
    },
    coercion_generator => sub {
        my ($base, $derived, $max) = @_;
        # $base - TruncatedString
        # $derived - TruncatedString[<N>]
        # $max - N
        
        # Not sure if I should be adding the coercion to $base or $derived, but I suspect that 
        # $derived is the correct choice here.
        $derived->coercion->add_type_coercions(Str, 
            sub {
                return substr $_, 0, $max;
            }
        );
    };

但是它对 Type-Tiny-1.012000 不起作用。我写了下面的测试来说明这个问题:

use Test2::V0;
use Time::Out qw< timeout >;
use Company::Types qw< TruncatedString >;

subtest 'Coercing to TruncatedString->of(10)' => sub {
    timeout 2 => sub {                      # test hangs without timeout
        my $type = TruncatedString->of(10); # same as TruncatedString[10]
        ok( try_ok { $type->coerce('123456789012') }, 'should not throw an exception' );
    } || bail_out( 'Ran out of time: ' . $@ );
};

done_testing();

这将输出以下内容:

# Seeded srand with seed '20201120' from local date.
Deep recursion on subroutine "Type::Tiny::_build_coercion" at ${SRC}/company-types/.direnv/perl5/lib/perl5/Type/Tiny.pm line 399.
Deep recursion on anonymous subroutine at ${SRC}/company-types/.direnv/perl5/lib/perl5/Type/Tiny.pm line 467.
Deep recursion on anonymous subroutine at ${SRC}/company-types/.direnv/perl5/lib/perl5/Type/Tiny.pm line 1015.
not ok 1 - Coercing to TruncatedString->of(10) {
    not ok 1
    # Failed test at t/truncated-string.t line 8.
    # Exception: CODE(0x7fee0fb78998)
    not ok 2 - should not throw an exception
    # Failed test 'should not throw an exception'
    # at t/truncated-string.t line 8.
    1..2
}

# Failed test 'Coercing to TruncatedString->of(10)'
# at t/truncated-string.t line 10.
1..1
Dubious, test returned 1 (wstat 256, 0x100)
Failed 1/1 subtests 

Test Summary Report
-------------------
t/truncated-string.t (Wstat: 256 Tests: 1 Failed: 1)
  Failed test:  1
  Non-zero exit status: 1
Files=1, Tests=1,  4 wallclock secs ( 0.02 usr  0.01 sys +  3.43 cusr  0.38 csys =  3.84 CPU)
Result: FAIL

我已将问题缩小到 coercion_generator (如果我将其注释掉,我将丢失此错误) 并已在 docs 中注明:

The following attributes are used for parameterized coercions, but are not fully documented because they may change in the near future:

我想这已经改变了,我希望得到一些关于如何更新我的现有代码以赶上进度的帮助?

Not sure if I should be adding the coercion to $base or $derived

都没有。你应该退货。

coercion_generator => sub {
    my ($base, $derived, $max) = @_;
    return Type::Coercion->new(
        type_coercion_map => [ Str, sub { substr $_, 0, $max } ]
    );
};

请参阅 Parameterizable Types in Type::Tiny::Manual::Libraries — 它提供了一个很好的解释和示例,说明如何使用内联和强制执行参数化类型。

我已经检查了当前 Type::Tiny 版本中的上述作品,甚至还有 0.006(2013 年 5 月发布)的旧版本!您在原始代码中的操作方式从来都不是 coercion_generator 的预期用途,我很惊讶它曾经起作用。


此外,如果您对为什么会收到关于深度强制转换的警告感兴趣,那是因为 $type->coercion 的行为很像 Moo/Moose 中的惰性属性,它调用你的 coderef,但是你的 coderef 叫 $type->coercion.